The method of transmitting value between JSP and Servlet

Source: Internet
Author: User
Tags url forwarding

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");

Request.setattribute ("Testrequest", "Hello request");

%>

<a href= "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, the href attribute of the <a> tag here and the value of the Action property of the <form> tag "Jspservlet?action=toservlet", in the servlet same Request.getparameter ("action") is obtained; Note that the URL here corresponds to the path of the servlet's <url-pattern> tag in Web. Xml. This section will be mentioned later.

3, Java fragment Code, servlet can only receive Session.setattribute ("Testsession", "Hello Session") of the content, but not 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) The JSP page Jspservlet1,url in the http://localhost:8080/jsp2servlet/directory should be written as "Jspservlet"

2) The JSP page access Jspservlet2,url in the http://localhost:8080/jsp2servlet/admin/directory 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

There are two ways to go from servlet to JSP, redirect and URL forwarding

1, redirect (Redirect): Is the path of the jump, the content and URL are changed. The request parameter (session parameter) is not allowed, that is, the request object is not allowed to be passed to the next page in the servlet using the SetAttribute method. Use the Response.sendredirect (URL) method in the servlet. Note that the URL here is preceded by a 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

Servlet uses RequestDispatcher to JSP page
String url= "/eshop.jsp";
ServletContext sc = Getservletcontext ();
RequestDispatcher rd = sc.getrequestdispatcher (URL);
Rd.forward (req, res);

The servlet redirects the JSP and sends the data to one of the other JSPs:
Request.setattubute ("Messages", AAA);
RequestDispatcher requestdispatcher=request.getrequestdispatcher ("/jsp/testbean.jsp");
Requestdispatcher.forward (Request,response);

3.7. servlet Jump
Jumping from JSP to servlet can be done, through form submission or hyperlinks, but now if you jump from one servlet to a JSP?
? HttpServletResponse object, this object can complete the jump: public void sendredirect ();
Jumpservlet.java:
Package Org.lxh.demo;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
public class Jumpservlet extends httpservlet{
public void Init () throws servletexception{
}
public void doget (HttpServletRequest req,httpservletresponse resp)
Throws servletexception,java.io.ioexception{
Resp.sendredirect ("jump.jsp");
}
public void DoPost (HttpServletRequest req,httpservletresponse resp)
Throws servletexception,java.io.ioexception{
This.doget (REQ,RESP);
}
public void Destroy () {
}
};
Xml:
<servlet>
<servlet-name>jump</servlet-name>
<servlet-class>org.lxh.demo.JumpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jump</servlet-name>
<url-pattern>/jump</url-pattern>
</servlet-mapping>
The above implementation of the jump function, but after jumping, found that the address bar has changed, so this jump is called the client jump. If a servlet is supposed to pass properties to the JSP at this point, then the session scope should be used.
Jumpservlet.java:
public void doget (HttpServletRequest req,httpservletresponse resp)
Throws servletexception,java.io.ioexception{
Request.getsession (). SetAttribute ("name", "Hello");
Resp.sendredirect ("jump.jsp");
}
JUMP.JSP:
At this point, it is found that the content can be passed through the session property scope, but if there is a lot of content now, and the session scope in the program will certainly bring additional performance degradation, but at this time, these values only use one display, it is certainly not necessary to use the Session object, It is best to use the request range. If the request attribute scope also has a limit, it must be a server-side jump, using the <jsp:forward> statement in the JSP, but in the servlet? Can only be done using the Requestdsipatcher interface, in fact the <jsp:forward> statement is also used to wrap the application, since RequestDispatcher is an interface, it must be instantiated by other means, Done through the Request object.
Jumpservlet.java:
public void doget (HttpServletRequest req,httpservletresponse resp)
Throws servletexception,java.io.ioexception{
Req.setattribute ("name", "Hello");
Ready to jump to this page
RequestDispatcher rd = Req.getrequestdispatcher ("jump.jsp");
Continue to pass all requests down.
Rd.forward (REQ,RESP);
}
Attention:
Use server-side jumps when you need to pass the request attribute range. If you do not need to pass, you can choose any of the jump methods.
But generally use requestdispatcher to jump, and must remember when the page address after the jump will not have any changes.

Forward and Sendredirect differences

1.request.getrequestdispatcher () is the request forwarding, the front and back page share a request;
Response.sendredirect () is redirected, and the front and back pages are not a request.

Request.getrequestdispather (); Returns a RequestDispatcher object.

2.requestdispatcher.forward () is running on the server side;
Httpservletresponse.sendredirect () is done by sending a command to the client's browser.
So Requestdispatcher.forward () is "Transparent" to the browser;
and Httpservletresponse.sendredirect () is not.

Reprinted from: http://blog.csdn.net/sinzen/article/details/54016724

The method of transmitting value between JSP and Servlet

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.