Http://topic.csdn.net/t/20040202/13/2694890.html
Q:
In action, the list is stored through request. setattribute (), but request. getattribute is not available in JSP. How can this problem be solved? Traditional Logic: How does iterator display set elements in JSP?
A:
Add it to the context as follows:
List list = new list ();
Servletcontext context = This. servlet. getservletcontext ();
Context. setattribute ("list", list );
You can get the following information:
List list = (list) getservlet (). getservletcontext (). getattribute ("list ");
In addition, http://zhidao.baidu.com/question/57874470.html
Getparameter () obtains the parameters passed in the URL. When the form of HTML is post, these parameters are passed implicitly, and the obtained object is string.
Getattribute () obtains the information returned to the page after action or servlet processing. You can use getattribute () to obtain the information on the page. as mentioned above, the value assigned by the setattribute () method is obtained, and the returned value is object.
When you create a specific project, each project has a query function. To put it simply:
When you click the query button on the query page, submit the form information to the processing class. The processing class is obtained using getparameter. then the processing class operation dB finds the matched record result set, and then uses the request. the setattribute ("list", list) method saves the result set in the request and returns it to the query page. getattribute ("list") method to obtain the result set, and then cyclically output the result to the page.
Sum up the pass-through between JSP and Servlet
There are two types of pass-through between JSP and Servlet: JSP-> servlet, servlet-> JSP.
Use the Object Request and Session (regardless of Application) to complete the value transfer.
I. jsp-> Servlet
The JSP page has three methods to pass values to the servlet: Form, URL, and others.
<! -- JSP page -->
...
<% ... ...
Session. setattribute ( " Testsession " , " Hello session " );
Reqeust. 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 content of the Form on the JSP page, such as the <input> tag, you can use request. getparameter ("username"); In the servlet.
2. url: for example, the <A> tag's href attribute and the <form> tag's action attribute value "jspservlet? Action = toservlet ", the request is also used in servlet. getparameter ("action") is obtained. Note that the URL and Servlet must be in the web. the path of the <URL-pattern> label in XML corresponds. This part will be mentioned later.
3. Java snippets Code , Servlet can only receive Session. setattribute (" Testsession " , " Hello session " ), But cannot receive the request content. Use request. getsession (). getattribute ("testsession") in servlet to get the session content.
Ii. servlet
1. For servlet, you should first mention its registration content in Web. XML, as shown in
< 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 the project name is jsp2servlet, the context of the project root directory is/jsp2servlet, http: // localhost: 8080/jsp2servlet/ is displayed in the address bar;
the project root directory contains the Admin directory. The corresponding context is/admin/jsp2servlet. the address bar displays HTTP: // localhost: 8080/jsp2servlet/admin ,
J in these two directories SP all want to go to the com. Demo. jspservletdemo class for processing. At this time, the URL needs to be registered twice in Web. xml.
1) in http: // localhost: 8080/jsp2servlet/ jspservlet1, the URL should be written as "jspservlet"
2) in HTTP: // localhost: 8080/jsp2servlet/admin/ access jspservlet2 on the JSP page under the directory, the URL should be "admin/jspservlet"
2. directly use the request object in the servlet to obtain the sent request content. Use request. getsession () to obtain the session object and obtain the session content.
Here, the request. getsession () parameter is of the boolean type. This method can be understood:
A session can be considered to correspond to one session for each ie process (two sessions can be created for each new ie process). getsession returns the session object of the current user. The parameter differences are as follows:
If the parameter is true (default), a new session object is created if the "current user's session object" is blank (when the first access is made;
If the parameter is false, if the "current user's session object" is null, null is returned (that is, the session object is not automatically created ).
This method can be used to determine whether the session has expired, as shown below:
If (Request. getsession ( False ) = Null )
System. Out. println ( " Session has been invalidated! " );
Else
System. Out. println ( " Session is active! " );
3. servlet-> JSP
Two methods are available for transferring from servlet to JSP: redirection and URL forwarding.
1. Redirect: the redirection of the path, and the content and URL change. The request parameter (the session parameter is acceptable) is not allowed, that is, the request object cannot be sent to the next page using the setattribute method in the servlet. Use the response. sendredirect (URL) method in the servlet. Note that the URL here does not contain a slash/, such as response. sendredirect ("test. jsp").
2. url Forwarding (forward): a page Jump. The page content changes and the URL remains unchanged. It can contain request and session parameters. Use getservletconfig (). getservletcontext (). getrequestdispatcher (URL). Forward (request, response) in servlet ). The URL here requires a slash (/), such as getservletconfig (). getservletcontext (). getrequestdispatcher ("/test. jsp"). Forward (request, response)