jsp原理

  首先先来理清下概念,jsp(java server page),jsp其实就是一个servlet,而servlet就是一个java类,而java类运行时会被编译成一个.class文件;

  有点不同的是在jsp中有java代码,有h5的代码,那么我们开看看jsp的.lass文件,可以到tomcat\ 项目\Catalina\localhost\Jstlcaseone\org\apache\jsp下查看,

先看jsp中代码:

  <body>
    <h1>case1 login</h1>
   
    <%
    	String message=(String)request.getAttribute("message");
    	if(message==null){
    	    message="";
    	}
    	
    %>
    <%
    String cvalue="";	
    Cookie[] cookies=request.getCookies();
    	if(cookies!=null){
    	    for(Cookie c:cookies){
    	        if(c.getName().equals("username")){
    	            cvalue=c.getValue();
    	        }
    	    }
    	}
    %>
    <font color="red"><b><%=message %></b></font>
    <form action="Login" method="post">
    	account:<input type="text" name="name" value="<%=cvalue%>"> <br/> 
    	password:<input type="password" name="password"><br/>
    	<input type="submit" value="submit">
    </form>
  </body>

在来看看jsp在java中的代码,编译成java文件,文件名为jsp名_java.java:

out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
      out.write("<html>\r\n");
      out.write("  <head>\r\n");
      out.write("    <base href=\"");
      out.print(basePath);
      out.write("\">\r\n");
      out.write("    \r\n");
      out.write("    <title>My JSP 'case1login.jsp' starting page</title>\r\n");
      out.write("    \r\n");
      out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
      out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
      out.write("\t<meta http-equiv=\"expires\" content=\"0\">    \r\n");
      out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
      out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
      out.write("\t<!--\r\n");
      out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
      out.write("\t-->\r\n");
      out.write("\r\n");
      out.write("  </head>\r\n");
      out.write("  \r\n");
      out.write("  <body>\r\n");
      out.write("    <h1>case1 login</h1>\r\n");
      out.write("   \r\n");
      out.write("    ");

    	String message=(String)request.getAttribute("message");
    	if(message==null){
    	    message="";
    	}
    	
    
      out.write("\r\n");
      out.write("    ");

    String cvalue="";	
    Cookie[] cookies=request.getCookies();
    	if(cookies!=null){
    	    for(Cookie c:cookies){
    	        if(c.getName().equals("username")){
    	            cvalue=c.getValue();
    	        }
    	    }
    	}
    
      out.write("\r\n");
      out.write("    <font color=\"red\"><b>");
      out.print(message );
      out.write("</b></font>\r\n");
      out.write("    <form action=\"Login\" method=\"post\">\r\n");
      out.write("    \taccount:<input type=\"text\" name=\"name\" value=\"");
      out.print(cvalue);
      out.write("\"> <br/> \r\n");
      out.write("    \tpassword:<input type=\"password\" name=\"password\"><br/>\r\n");
      out.write("    \t<input type=\"submit\" value=\"submit\">\r\n");
      out.write("    </form>\r\n");
      out.write("  </body>\r\n");

可以看到但凡是h5的代码,在java文件中,都用out.write()来输出  而<%%>中的代码则是原封不动的出现在java中,这就是jsp的真面目;


上个图便于理解,其实就是当客户端发起请求时,第一次访问该jsp时,则服务器做了后面4步,再次访问时,只要调用起service方法就可以了;画的不是很准确;

猜你喜欢

转载自blog.csdn.net/qq_39512671/article/details/80356595
今日推荐