目次へ

解答例 - 実習課題3 - 1.JSPの基本

(実習課題3)

以下のJSPページを作成しなさい。

  • <%! %>タグと<% %>タグ内に変数を宣言し、ページが処理される度に値を1ずつ増やす事。
  • その値を<%= %>タグで出力すること。

変換されてできたサーブレットプログラムと表示される値の関係を確認する事。

解答例

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;

public class script_jsp extends HttpJspBase {

 int j = 0;

  private static java.util.Vector _jspx_includes;

  public java.util.List getIncludes() {
    return _jspx_includes;
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    JspFactory _jspxFactory = null;
    javax.servlet.jsp.PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;


    try {
      _jspxFactory = JspFactory.getDefaultFactory();
      response.setContentType("text/html; charset=Windows-31J");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("<!-- script.jsp -->\r\n");
      out.write("<!-- TECHSCORE Java JSP 1章 実習課題3 -->\r\n");
      out.write("<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->\r\n\r\n");
      out.write("\r\n");
      out.write("<html>\r\n  ");
      out.write("<head>");
      out.write("<title>JSP 1章 実習課題 3");
      out.write("</title>");
      out.write("</head>\r\n  ");
      out.write("<body>\r\n  ");
      out.write("<h3>JSP 1章 実習課題 3");
      out.write("</h3>\r\n    ");
      out.write("<p><%>の場合");
      out.write("<br/>\r\n      ");
 int i = 0;
      out.write("\r\n      ");
      out.print( i++ );
      out.write("\r\n    ");
      out.write("</p>\r\n    ");
      out.write("<p><%!>の場合");
      out.write("<br/>\r\n      ");
      out.write("\r\n      ");
      out.print( j++ );
      out.write("\r\n    ");
      out.write("</p>\r\n  ");
      out.write("</body>\r\n");
      out.write("</html>\r\n\r\n");
    } catch (Throwable t) {
      out = _jspx_out;
      if (out != null && out.getBufferSize() != 0)
        out.clearBuffer();
      if (pageContext != null) pageContext.handlePageException(t);
    } finally {
      if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
    }
  }
}
<!-- script.jsp -->
<!-- TECHSCORE Java JSP 1章 実習課題3 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@ page contentType="text/html; charset=Windows-31J" %>
<html>
  <head><title>JSP 1章 実習課題 3</title></head>
  <body>
  <h3>JSP 1章 実習課題 3</h3>
    <p><%>の場合<br/>
      <% int i = 0;%>
      <% i++; %>
      <%= i %>
    </p>
    
<p><%!>の場合<br/>
      <%! int j = 0;%>
      <% j++; %>
      <%= j %>
    </p>
  </body>
</html>

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp