Two types of value-transfer between servlet and JSP _java

Source: Internet
Author: User
Tags url forwarding
There are two things that are passed between the servlet and the JSP: the JSP-> servlet, the servlet-> jsp.
The pass value is completed through object request and session (regardless of application).

First, JSP-> servlet
JSP page There are 3 ways to send values to the servlet: form forms, URLs
Copy Code code as follows:

<!--JSP page-->
...
<% ...
Session.setattribute ("Testsession", "Hello session");
Reqeust.setattribute ("Testrequest", "Hello request");
%>
<a href= "Jspservlet?action=toservlet" > Click </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 can be Request.getparameter ("username");
2, URL: For example, here's the <a> tag's href attribute and <form> tag's action attribute value "Jspservlet?action=toservlet", also used in the servlet Request.getparameter ("action") to note that the URL here corresponds to the web.xml of the servlet in the path of the <url-pattern> tag. This part will be mentioned later.
3, Java fragment Code, the servlet can only receive Session.setattribute ("Testsession", "Hello session") content, but not the content of the request. Gets the session content in the servlet using Request.getsession (). getattribute ("Testsession").

Second, the Servlet
1, on the servlet, first mention it in the Web.xml register content, such as
Copy Code code as follows:

<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 appears in the Address bar as http://localhost:8080/jsp2servlet/;
In the project root directory has the admin directory, the corresponding context is/admin/jsp2servlet, in the Address bar show is Http://localhost:8080/jsp2servlet/admin,
The JSP in both directories wants to go to the Com.demo.JspServletDemo class for processing, at which point the URL needs to be registered two times in Web.xml.
1 in the http://localhost:8080/jsp2servlet/directory of JSP page Jspservlet1,url should be written as "Jspservlet"
2 in the http://localhost:8080/jsp2servlet/admin/directory of JSP page access Jspservlet2,url should be written as "Admin/jspservlet"
2, in the servlet directly with the request object, get the requested content sent, with Request.getsession (), get the Session object, so that the conversation content.
The argument for the Request.getsession () here is a Boolean type, which can be understood to mean:
Session can be considered as each IE process corresponding to a conversation (a new IE process can correspond to two sessions), GetSession are returned to 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;
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 whether the session expires, as follows:
Copy Code code as follows:

if (Request.getsession (false) ==null)
SYSTEM.OUT.PRINTLN ("Session has been invalidated!");
Else
System.out.println ("Session is active!");

third, Servlet-> JSP
Go from servlet to JSP in two ways, redirection and URL forwarding
1, redirect (Redirect): Is the path of the jump, content and URL are changed. The request parameter (the session parameter can) is not allowed, i.e. the setattribute method is not allowed to be passed to the next page in the servlet to the request object. Use the Response.sendredirect (URL) method in the servlet. Note that the URL here is not with a slash/, such as Response.sendredirect ("test.jsp")
2, URL forwarding (Forward): Is the page's jump, the page content changes, the URL unchanged. You can bring the request and session parameters. Use the Getservletconfig (). Getservletcontext (). Getrequestdispatcher (URL) in the servlet. Forward (request, response). And here the URL needs to be with a slash/, such as Getservletconfig (). Getservletcontext (). Getrequestdispatcher ("/test.jsp"). Forward (Request, Response
Copy Code code as follows:

String fr= "Good OK";
Request.setattribute ("Test", FR);
RequestDispatcher de=request.getrequestdispatcher ("/test.jsp");
De.forward (request, response);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.