TomCat:JSPと連携するwebサーバー
<html> <head><title>Hello World!</title></head> <body> <h1>Hello World!</h1> <p> <% String hello = "Hello World!"; out.println(hello+1+"<BR>"); out.println(hello+2); %> </p> </body> </html>
package org.apache.jsp.ex; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static java.util.Vector _jspx_dependants; public java.util.List getDependants() { return _jspx_dependants; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try { _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; out.write("<html>\r\n"); out.write("<head><title>Hello World!</title></head>\r\n"); out.write("<body>\r\n"); out.write("<h1>Hello World!</h1>\r\n"); out.write("<p>\r\n"); String hello = "Hello World!"; out.println(hello+1+"<BR>"); out.println(hello+2); out.write("\r\n"); out.write("</p>\r\n"); out.write("</body>\r\n"); out.write("</html>\r\n"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) out.clearBuffer(); if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } } }
<HTML> <HEAD><TITLE>jsp Form</TITLE> </HEAD> <BODY> <FORM ACTION="ex2.jsp" METHOD="POST"> <TABLE> <TR><TD>name</TD><TD><input type="text" name="name"></td></tr> <TR><TD>age</TD><TD><input type="text" name="age"></td></tr> </TABLE> <INPUT TYPE="SUBMIT" VALUE="go"><BR> </FORM> </HTML>これを処理する Jsp のページの例を示します。<%@ page %> はディレクティブと呼ばれ、ページを生成する際の情報を設定します。次のスクリプトレット <% %> で、Form タグの名前に対するデータをstrNameとstrAgeに取得しています。<%= %> は式で、=以後の文字列を出力します。これで、Formタグの値を取り込み、再度表として出力します。
<%@ page language="java" %> <%@ page contentType="text/html;charset=Shift_JIS"%> <% request.setCharacterEncoding("SHIFT_JIS"); String strName=request.getParameter("name"); String strAge=request.getParameter("age"); %> <HTML> <HEAD><TITLE>jsp cgi</TITLE> </HEAD> <BODY> <h3>表の作成</h3> <TABLE border=1> <TR><TD>name</TD><TD><%=strName%></td></tr> <TR><TD>age</TD><TD><%=strAge%></td></tr> </TABLE> </HTML>
String message=request.getParameter("message"); int count=Integer.parseInt(request,getparameter("count"); <TABLE> <TR><TD>回数</TD><TD>メッセージ</td></TR> <% for (int i=0; i < count;i++){ %> <TR><TD><%= i+1 %></TD><TD><%= message %></TD></TR> <% } %> </TABLE>