1.静态HTML文件ourform.html

  <html>
  <head>
  <title>Our Form</title>
  </head>
  <body>
  <h3>Our Form</h3>
  <form name="ourform" method="GET" action="JSPtest.jsp">
  <table>
    <tr>
      <td>姓名:</td>
      <td><input type=text name="Name"></td>
    </tr>
    <tr>
      <td>性别:</td>
      <td><select name="Sex">
        <option value="1" selected>男</option>
        <option value="2">女</option>
      </select></td>
    </tr>
  </table>
  </form>
  </body>
  </html>

 2.JSP文件:JSPtest.jsp

  <%@ page contentType="text/html; charset=gb2312" %>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <title>Your Info</title>
  </head>
  <body>
  <h3>Data You Posted</h3>
  <%
    String name = request.getParameter("Name");
    String sex = request.getParameter("Sex");
  %>
  <table>
  <tr>
  <td>你的姓名:</td>
  <td><%=name%></td>
  </tr>
  <tr>
  <td>你的性别:</td>
  <td><% if(sex.equals("1")) out.print("男");
    else out.print("女");%></td>
  </tr>
  </table>
  </body>
  </html>

  这个例子的执行结果和前面是一摸一样的。在以前Servlet分析的基础上,读者看懂这个例子应该没什么问题,这里就不对语法做过多分析了。下面我们来分析一下这个例子的几个特点。

  首先最明显的一点就是,使用JSP之后文件变得更短,格式更清晰了,这也是要使用JSP的一个最主要的原因,使用Servlet来打印大量的HTML语句是很费事的,而JSP的主体是HTML,嵌入的Java语句只负责动态效果,所以使用比Servlet方便的多。在下面我们会分析如何使Servlet和JSP互相配合,取长补短,获得更好的应用效果。

  另外一个就是JSP使用的时候不需要单独配置每一个文件,只要是扩展名为jsp,JSP引擎会自动识别。而Servlet是必须进行配置后才能投入使用的,这也是处于安全性的考虑,直接访问.class文件是不允许的,因为不能保证它是一个合法的Servlet。而JSP是没有经过编译的文本,即使是编译成了Servlet,也肯定是符合Servlet规范的,尽管可能不符合HTML语法,所以它是安全的。

  还有就是中文问题在这里的到了简化,本地的中文字符串不需要编码转换就能够正常在客户端显示。这里关键的一点是在头部设置页属性<%@ page contentType="text/html; charset=gb2312" %>,其中charset=gb2312就是告诉JSP引擎本地编码是gb2312,然后JSP引擎就会自动进行转换,不需要手工转了。但是,并不是说就这么一劳永逸了,中文问题在JSP页面之间传参的时候还是存在的,读者只要掌握了编码的转换方式,应付任何中文问题就是转来转去的问题,因此读者应该从根本上理解中文问题,这才是一劳永逸的办法。

  这时候如果我们把静态页面的GET改成POST会是什么后果呢?我们会发现,不象HttpServlet,这个JSP还是能够正常工作的,这是因为JSP的实现在最低层是Servlet,但跟HttpServlet又是有所区别的,所以它不存在客户端请求的分类问题,它只有一个_jspService(HttpServletRequest request, HttpServletResponse response)方法由JSP引擎实现,所以写JSP不必象Servlet那样关心那么多杂碎的事情。

  下面是Resin生成的临时Java文件,是上面的例子转换成Servlet之后的样子,有兴趣的读者可以读一下,如果真正读通懂了,一定会受益匪浅的。

  // Resin(tm) generated JSP
  package _jsp;
  import java.io.*;
  import javax.servlet.*;
  import javax.servlet.jsp.*;
  import javax.servlet.jsp.tagext.*;
  import javax.servlet.http.*;

  public class _JSPtest__jsp extends com.caucho.jsp.JavaPage{
    
    public void
    _jspService(javax.servlet.http.HttpServletRequest request,
        javax.servlet.http.HttpServletResponse response)
     throws IOException, javax.servlet.ServletException
    {
     javax.servlet.jsp.PageContext pageContext = com.caucho.jsp.QJspFactory.create().getPageContext(this, request, response, null, true, 8192, true);
     javax.servlet.jsp.JspWriter out = (javax.servlet.jsp.JspWriter) pageContext.getOut();
     com.caucho.jsp.ByteWriteStream _jsp_raw_out;
     _jsp_raw_out = (com.caucho.jsp.ByteWriteStream) out;
     javax.servlet.ServletConfig config = getServletConfig();
     javax.servlet.Servlet page = this;
     javax.servlet.http.HttpSession session = pageContext.getSession();
     javax.servlet.ServletContext application = pageContext.getServletContext();
  try {
     _jsp_raw_out.write(_jsp_string0, 0, _jsp_string0.length);

     String name = request.getParameter("Name");
     String sex = request.getParameter("Sex");
     _jsp_raw_out.write(_jsp_string1, 0, _jsp_string1.length);
       out.print((name));
       _jsp_raw_out.write(_jsp_string2, 0, _jsp_string2.length);
        if(sex.equals("1")) out.print("男");
        else out.print("女");_jsp_raw_out.write(_jsp_string3, 0, _jsp_string3.length);
    } catch (Exception e) {
     pageContext.handlePageException(e);
    } finally {
      JspFactory.getDefaultFactory().releasePageContext(pageContext);
    }
  }

  private java.util.ArrayList _caucho_depends;
  private java.util.ArrayList _caucho_cache;
  private com.caucho.java.LineMap _caucho_line_map;

  public boolean _caucho_isModified()
  {
   if (com.caucho.util.CauchoSystem.getVersionId() != -1983231406)
    return true;
   for (int i = 0; i < _caucho_depends.size(); i++) {
    com.caucho.jsp.Depend depend;
    depend = (com.caucho.jsp.Depend) _caucho_depends.get(i);
    if (depend.isModified())
      return true;
  }
  return false;
  }

  public long _caucho_lastModified()
  {
   return 0;
  }

  public com.caucho.java.LineMap _caucho_getLineMap()
  {
   return _caucho_line_map;
  }

  public void _caucho_init(HttpServletRequest req, HttpServletResponse res)
  {
   res.setContentType("text/html; charset=GB2312");
  }

  public void init(ServletConfig config,
      com.caucho.java.LineMap lineMap,
      com.caucho.vfs.Path pwd)
  throws ServletException
  {
   super.init(config);
   _caucho_line_map = new com.caucho.java.LineMap("_JSPtest__jsp.java", "/JSPtest.jsp");
   _caucho_line_map.add(1, 1);
   _caucho_line_map.add(1, 28);
   _caucho_line_map.add(9, 29);
   _caucho_line_map.add(16, 33);
   _caucho_line_map.add(20, 35);
   _caucho_depends = new java.util.ArrayList();
   _caucho_depends.add(new com.caucho.jsp.Depend(pwd.lookup("/D:/gzf@WeiHai/Resin/doc/JSPtest.jsp"),
   996991110000L));
  }

  private static byte []_jsp_string0;
  private static byte []_jsp_string1;
  private static byte []_jsp_string2;
  private static byte []_jsp_string3;
  static {
    try {
      _jsp_string0 = "\r\n<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html;    charset=gb2312\">\r\n<title>Your Info</title>\r\n</head>\r\n<body>\r\n<h3>Data You Posted</h3>\r\n".getBytes("GB2312");
      _jsp_string1 = "\r\n<table>\r\n<tr>\r\n<td>你的姓名:</td>\r\n<td>".getBytes("GB2312");
      _jsp_string2 = "</td>\r\n</tr>\r\n<tr>\r\n<td>你的性别:</td>\r\n<td>".getBytes("GB2312");
      _jsp_string3 = "</td>\r\n</tr>\r\n</table>\r\n</body>\r\n</html>\r\n".getBytes("GB2312");
    } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
    }
  }
}

  到这里为止,JSP这部分已经讲解完毕,读者对JSP应该有了一个概念上的认识,应该有能力书写简单的JSP文件。如果对某些概念还不是很清楚,也不必着急,多多练习就会有更深的体会。下面我们会对JSP和Servlet的关系做进一步的讨论,希望能帮助读者更好的理解Servlet和JSP。