There are two types of values between JSP and servlet: JSP, Servlet, servlet, and jsp.
Pass through the object request and session (regardless of application) to complete the pass value.
A,JSP --servlet
There are 3 ways to pass values to a servlet on a JSP page : Form form,URL, other
<!--JSP Page--
...
<% ...
Session.setattribute ("Testsession", "Hello session");
Reqeust.setattribute ("Testrequest", "Hello request");
%>
<a href= "Http://xuchuanhou.blog.163.com/blog/JspServlet?action=toServlet" >click me</a>
<form action= "Jspservlet?action=toservlet" method= "post" name= "form" >
<input name= "username" type= "test"/>
<input type= "Submit" value= "Submit" >
</form>
...
1, for the JSP page form form content, such as <input> tags, in the servlet available request.getparameter ("username");
2, url: for example, <a> label href attribute with <form> label action property value "Jspservlet?action=toservlet", in servlet the same request.getparameter ("action"); url to and servlet in web.xml" <url-pattern> The path of the label corresponding. This section will be mentioned later.
3,Java fragment code,servlet can only receive Session.setattribute ("Testsession", "Hello Session") of the content, and cannot receive The content of the request . use request.getsession (). getattribute ("Testsession") in the servlet to get the session content.
Second,Servlet
1. For Servlets, the first thing to mention is its registered content in Web. XML, such as
<servlet-name>JspServlet1</servlet-name>
<servlet-class>com.demo.JspServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JspServlet1</servlet-name>
<url-pattern>/JspServlet</url-pattern>
</servlet-mapping>
<servlet-name>JspServlet2</servlet-name>
<servlet-class>com.demo.JspServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JspServlet2</servlet-name>
<url-pattern>/admin/JspServlet</url-pattern>
</servlet-mapping>
If project name is Jsp2servlet, the context of the project root directory is /jsp2servlet, which is displayed in the Address bar http://localhost : 8080/jsp2servlet/;
In the project root directory has the Admin directory, the corresponding context is /admin/jsp2servlet, in the Address bar display is http://localhost:8080/ Jsp2servlet/admin,
In both directories , the JSP wants to go to the Com.demo.JspServletDemo class to do the processing, when the URL needs to be registered in Web. XML two times.
1) in the http://localhost:8080/jsp2servlet/ directory of the JSP page JspServlet1, theURL should be written as "Jspservlet"
2) in the http://localhost:8080/jsp2servlet/admin/ directory of the JSP page access JspServlet2, theURL should be written as "admin/ Jspservlet "
2, in the servlet directly with the request object, get the requested content sent, with request.getsession (), to get the session object, so as to get the conversation content.
The parameter of Request.getsession () here is a boolean type, which can be understood as:
Session can be considered that each IE process corresponds to a session ( the new IE process can correspond to two sessions ),GetSession is to return the current user's session object, the difference between the parameters:
parameter is true (default ), if the "current user's Session object " is empty (on first access ), a new session object is created to return;
argument is false, NULL is returned if the current user's session object is empty (that is, the session object is not created automatically ).
Use this method to determine if the session expires, as follows:
if (Request.getsession (false) ==null)
System.out.println ("Session has been invalidated!");
Else
System.out.println ("Session is active!");
Third,Servlet --JSP
from servlet go to JSP is only two methods, redirect and url forward
1, redirected (Redirect) : Is the path of the jump, content and url all changed. Not allowed with request parameters (session parameters can be servlet "en-our" XML is not allowed in request objects are passed to the next page using setattribute methods. In servlet using response.sendredirect (URL) method. Note here the url before the slash /, such as response.sendredirect ("test.jsp")
2,URL forwarding (Forward): Is the page jump, page content changes,URL unchanged. You can bring the request and session parameters. In the servlet , use Getservletconfig (). Getservletcontext (). Getrequestdispatcher (URL). Forward (request, response). The URL here needs to be preceded by a slash /, such as Getservletconfig (). Getservletcontext (). Getrequestdispatcher ("/test.jsp"). Forward ( Request, Response)
Parameter passing "Go" between JSP and Servlet