Doget andThe difference between dopost
Get andPost isTwo methods of HTTP protocol, plusHead, DELETE, etc.
There are essential differences between the two methods,Get has only one stream, parameters attached to theAfter a URL, the number of sizes is strictly limited and can only be strings.The parameters of the post are passed through a different stream, not throughURL, so can be very large, can also pass binary data, such as file upload.
InIn servlet development, theDoget () andDoPost () handle separatelyGet andPost method.
The first time you judge the request isGet orPost, if it isGet is calledDoget (), if yesPost is calledDoPost (). Will execute this method.
1.doGet
A get call is used to get server information and to return it as a response to the client. When throughWeb browser or byHtmlJSP Direct AccessServlet'sURL, it is generally usedGet call.Get calls inThe URL shows the story sent toservlet data, which may cause some problems in the security aspects of the system, such as user login, the user name and password in the form need to be sent to the server side, if using get call, the user name and password will be displayed in the browser's url. Example:
JSP page code:
<form action= "/doget_servlet" Name= "Form1" method= "get";
...
<input type= "text" name= "username";
...
</form>
servlet code:
public class Doget_servlet extends HttpServlet {
public void doget (httpservletrequest request,httpservletresponse response) throws ioexception,servletexception {
request.setcaracterencoding ("UTF-8");// Kanji transcoding
string username = Request.getparameter ("username");
Request.setattribute ("username", username);
Request.getrequestdispatcher ("/out.jsp"). Forward (request, response);//Jump to out.jsp page
}
}
out.jsp page
``````
<%=request.getattribute ("username")%>//output username information on the page
When the form is submitted, the parameters are automatically added to the browser's address bar, resulting in security issues.
2.doPost
It is used for the client to transfer data to the server side, there will be side effects. But the advantage is that you can hide any data that is routed to the server.Post is suitable for sending large amounts of data.
Cases:
JSP page code:
<form action= "/dopostt_servlet" name= "Form2" method= "POST" >
.........
<textarea name= "name2" cols= "ten" rows= "></textarea>"
.........
</form>
Servlet Code:
public class Dopostt_servlet extends HttpServlet {
public void DoPost (HttpServletRequest request,httpservletresponse esponse) throws Ioexception,servletexception {
Request.setcaracterencoding ("UTF-8");//Kanji transcoding
PrintWriter out = Response.getwriter ();
Out.println ("The Parameter is:"+request.getparameter (" name2 "));
}
}
It is best to output using the output method mentioned above in Doget
3. The method can be written inIn the Doget () methodInThe DoPost () method calls the execution, so, no matter what you submit isPost orThe Get method can be executed
For example:
JSP page code:
<form action= "/servlet" name= "form" method= "POST" >
.........
<input type= "text" name= "name1" >
.........
</form>
Servlet Code:
public class Servlet extends HttpServlet {
public void doget (HttpServletRequest request,httpservletresponse response) throws Ioexception,servletexception {
Request.setcaracterencoding ("UTF-8");//Kanji transcoding
printwriter out = Response.getwriter ( );
out.println ("The Parameter are : "+request.getparameter (" name1 "));
}
public void DoPost (httpservletrequest request,httpservletresponse response) throws IOException, servletexception {
this.goget (request,response);//Call doget () method
}
}
In addition, httpservlet handles client requests in the doput, dodelete, dotrace, dohead, dooptions, but less used.
The difference between doget and dopost