The basic principle is to use the setattribute () and getattribute () methods in the session to save and obtain a parameter.
1. create a web project under my eclipse, and create JSP files for two sessions in the default path, where session. JSP is used to submit the form content, and session1.jsp is used to obtain the parameter content saved by the session.
2. Create a Servlet File to process the Form Content submitted by session. jsp and put it in the request. setattribute () method.
Note: The setattribute () and getattribute () methods under the session cannot be directly used in servlet. the header file must contain javax. servlet. HTTP. * when creating a session object, you must declare this object first. The syntax is as follows:
HTTP session = request. getsession ();
The Code is as follows:
Session. jsp
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "ISO-8859-1" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
</Head>
<Body>
<Form action = "form"> & nbsp;
A: <input type = "text" name = "A" size = 5> <p> & nbsp;
B: <input type = "text" name = "B" size = 5> <p> & nbsp;
C: <input type = "text" name = "C" size = 5> <p> & nbsp;
<Input type = submit value = "Submit">
</Form>
</Body>
</Html>
Session1.jsp
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "ISO-8859-1" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Body>
<%
List <string> array = new arraylist (); // list is used to save every value in the form.
Array = (list <string>) request. getattribute ("array ");
Out. Print ("<H2> session save a, B, c: </H2> <p>"); // JDK
For (string name: array) // foreach syntax supported by Versions later than jdk1.4
{
Out. println (name );
Out. Print ("<p> ");
}
%>
</Body>
</Html>
Form. Java // Servlet File
Import java. Io. ioexception;
Import java. Io. printwriter;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http .*;
Import java. util .*;
Public class form extends httpservlet {
/**
* Constructor of the object.
*/
Public form (){
Super ();
}
/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. Destroy (); // just puts "Destroy" string in log
// Put your code here
}
/**
* The doget method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to get.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws servletexception if an error occurred
* @ Throws ioexception if an error occurred
*/
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
String A = (string) request. getparameter ("");
String B = (string) request. getparameter ("B ");
String c = (string) request. getparameter ("C ");
List <string> array = new arraylist ();
Array. Add (a); array. Add (B); array. Add (C );
Httpsession session = request. getsession (true );
Session. setattribute ("array", array );
Request. setattribute ("array", session. getattribute ("array "));
Getservletconfig (). getservletcontext (). getrequestdispatcher (
"/Session1.jsp"). Forward (request, response );
}
/**
* The dopost method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to post.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws servletexception if an error occurred
* @ Throws ioexception if an error occurred
*/
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
/**
* Initialization of the servlet. <br>
*
* @ Throws servletexception if an error occurs
*/
Public void Init () throws servletexception {
// Put your code here
}
}
The web project structure created in my eclipse is as follows: