1, the foreground JSP code himily.jsp, defines the user name and the password two input box, uses the Post method to Submit:/order-web/himilyservlet where Order-web is the site name, Himilyservlet Access path for servlet
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="Utf-8"%><!DOCTYPE HTML public "-//w3c//dtd HTML 4.01 transitional//en" "http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "content-type"content= "text/html; charset=utf-8"><title>Welcome to Here</title></Head><Body> <formAction= "/order-web/himilyservlet"Method= "Post">User Name:<inputtype= "text"value= "please Enter User name"name= "user"size= "20px"><BR>password:<inputtype= "password"value= "please Enter password"name= "pswd"size= "20px"><BR> <inputtype= "submit"value= "submit"size= "10px"> </form></Body></HTML>
himily.jsp
2. servlet layer Code: Myservlet.java
Creates a new Myservlet and specifies that the path to the servlet Is/himilyservlet
<! DOCTYPE web-app public"-//sun microsystems, inc.//dtd Web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app > <display-name>archetype Created Web application</display-name> <servlet> <servlet-name> ;orderservlet</servlet-name> <display-name>OrderServlet</display-name> <description>< /description> <servlet-class>com.suning.logistics.order.web.orderservlet</servlet-class> </servlet> <servlet> <servlet-name>QueryOrder</servlet-name> <display-name> Queryorder</display-name> <description></description> <servlet-class>com.suning.logistics.order.web.queryorder</servlet-class> </servlet> <servlet> <servlet-name>MyServlet</servlet-name> <display-name>m Yservlet</display-name> <description></description> <servlet-class>com.suning.logistics.order.web.myservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>OrderServlet</servlet-name> <url-pa ttern>/orderservlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name> Queryorder</servlet-name> <url-pattern>/queryorder. do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>myservlet</ Servlet-name> <url-pattern>/HimilyServlet</url-pattern> </servlet-mapping></web-app>
Web. XML
Myservlet.java code, Here is the request to accept the client and the response after processing
Method One:
map<string,string[]> PS = Request.getparametermap ();
The key and value values for all request parameters of the client are stored in a map, and value is an array type, making it easy to make a two-dimensional array with the same key
1 packagecom.suning.logistics.order.web;2 3 Importjava.io.IOException;4 Importjava.io.PrintWriter;5 Importjava.security.Principal;6 Importjava.util.Map;7 8 Importjavax.servlet.ServletException;9 Importjavax.servlet.http.HttpServlet;Ten Importjavax.servlet.http.HttpServletRequest; one Importjavax.servlet.http.HttpServletResponse; a - /** - * Servlet Implementation class Myservlet the */ - public classMyservletextendsHttpServlet { - Private Static Final LongSerialversionuid = 1L; - + /** - * @seeHttpservlet#httpservlet () + */ a publicMyservlet () { at Super(); - //TODO auto-generated Constructor stub - } - - /** - * @seehttpservlet#doget (httpservletrequest request, httpservletresponse response) in */ - protected voidDoget (httpservletrequest request, HttpServletResponse Response)throwsservletexception, IOException { to Try{ + //System.out.println ("himily ' s servlet!"); - //response.getwriter (). Write ("aaaa"); theRequest.setcharacterencoding ("utf-8"); *Response.setcontenttype ("text/html;charset=utf-8"); $String user = Request.getparameter ("user");Panax NotoginsengString pswd = Request.getparameter ("pswd"); -Response.setstatus (200); thePrintWriter pw=Response.getwriter (); +Pw.write ("); a pw.close (); the}Catch(Exception E) { + response.getwriter (). Write (e.tostring ()); - } $ $ - } - the /** - * @seehttpservlet#dopost (httpservletrequest request, httpservletresponse response)Wuyi */ the protected voidDoPost (httpservletrequest request, HttpServletResponse Response)throwsservletexception, IOException { - //TODO auto-generated Method Stub wu //doget (request,response); -map<string,string[]> PS =Request.getparametermap (); aboutRequest.setcharacterencoding ("utf-8"); $Response.setcontenttype ("text/html;charset=utf-8"); - //String user = Request.getparameter ("user"); - //String pswd = Request.getparameter ("pswd"); -Response.setstatus (200); aPrintWriter pw=Response.getwriter (); +Pw.write ("); the pw.close (); - } $ the}
Myservlet.java
Method Two:
Get the value of the client post parameter
String user = Request.getparameter ("user");
String pswd = Request.getparameter ("pswd");
protected voidDoPost (httpservletrequest request, HttpServletResponse Response)throwsservletexception, IOException {//TODO auto-generated Method Stub//Doget (request,response); //map<string,string[]> PS = request.getparametermap ();Request.setcharacterencoding ("utf-8"); Response.setcontenttype ("text/html;charset=utf-8"); //get the value of the client post parameterString user = Request.getparameter ("user"); String pswd= Request.getparameter ("pswd"); Response.setstatus (200); PrintWriter PW=response.getwriter ();//output response information to the clientPw.write ("); Pw.close (); }
Servlet Method Two
3. Access Address:
Post Access address Is: http://localhost:9008/order-web/himily.jsp
Get mode access Address: HTTP://LOCALHOST:9008/ORDER-WEB/HIMILYSERVLET?USER=AAA&PSWD=BBBB
Direct access to servlet address: Http://localhost:9008/order-web/HimilyServlet
Java in the foreground JSP request Servlet Instance (http+servlet)