PackageCn.edu.aynu.rjxy.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classAservletextendsHttpServlet {/*method Called when a GET method is requested * what is a GET request? * Enter the username and password in the Address bar, hyperlink, directly in the method to write the Get * Get no request body cannot set the request body * First need to get to username and PSW * and then put the string back to the byte array * using iso-8859- 1 encoding decoding * to reassemble a byte array into a string, using the UTF-8 encoding method **/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("UTF-8"); //get parameter values based on parameter nameString username = request.getparameter ("username"); String PSW= Request.getparameter ("PSW"); //use Iso-8859-1 to rewind the value of username to a byte array byte[] B = username.getbytes ("Iso-8859-1"); //to re-decode a byte array using UTF-8Username =NewString (b, "UTF-8"); //the response output stream is encoded by the UTF-8 client, and the browser is decoded in the same way as UTF-8Response.setcontenttype ("Text/html;charset=utf-8"); //input to the interfaceResponse.getwriter (). Print ("username=" +username); Response.getwriter (). Print ("Psw=" +PSW); //input to control tableSystem.out.println ("username=" +username); System.out.println ("Psw=" +PSW); } //method of Post mode request invocation Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {/*The reason why the decoding of Post request is garbled is that the encoding method and decoding method are inconsistent. * Tomcat Server default decoding method is iso-8859-1 does not support Chinese. Because the POST request * has the request body, the request parameter is placed in the request body, sets the request body decoding way, needs to call the * method Request.setcharacterencoding ("UTF-8"); support Chinese*/request.setcharacterencoding ("UTF-8"); //get parameter values based on parameter nameString username = request.getparameter ("username"); String PSW= Request.getparameter ("PSW"); Get more than one parameter value based on the name of the parameter
string[] Hobby= Request.getparametervalues ("Hobby"); Traversal of an array
for(inti = 0; i < hobby.length; i++) {System.out.println (hobby[i]); } //the response output stream is encoded by the UTF-8 client, and the browser is decoded in the same way as UTF-8Response.setcontenttype ("Text/html;charset=utf-8"); //input to the interfaceResponse.getwriter (). Print ("username=" +username); Response.getwriter (). Print ("Psw=" +PSW); //input to control tableSystem.out.println ("username=" +username); System.out.println ("Psw=" +PSW); }}
Aservlet.java
a.jsp
<%@ Pagelanguage= "Java"Import= "java.util.*"pageencoding= "UTF-8"%><%stringPath= Request.getcontextpath ();String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <Basehref= "<%=basePath%>"> <title>My JSP ' a.jsp ' starting page</title> <Metahttp-equiv= "Pragma"content= "No-cache"> <Metahttp-equiv= "Cache-control"content= "No-cache"> <Metahttp-equiv= "Expires"content= "0"> <Metahttp-equiv= "keywords"content= "Keyword1,keyword2,keyword3"> <Metahttp-equiv= "description"content= "This is my page"> <!--<link rel= "stylesheet" type= "Text/css " href= "Styles.css" > - </Head> <Body> <formAction= "/day9/aservlet"Method= "POST">User name:<inputtype= "text"name= "username"/><BR/>Password:<inputtype= "Password"name= "PSW"/><BR/>Hobbies:<inputtype= "checkbox"name= "Hobby"value= "Swimming">Swimming<inputtype= "checkbox"name= "Hobby"value= "Sing">singing<inputtype= "checkbox"name= "Hobby"value= "Run">Running<inputtype= "checkbox"name= "Hobby"value= "Read">Read<BR> <inputtype= "Submit"value= "Submit"> </form> <HR> <ahref= "/day9/aservlet?username= John Doe &psw=123">Get method Request</a> </Body></HTML>
Java Web Solution parsing garbled and responding garbled