Servlet--httpservletrequest some ways about paths

Source: Internet
Author: User

When we interact with the form using a servlet, both the pass-through and the reference often write paths. About the specific servlet's pass-through and the connection parameter I will have the detailed arrangement later, here first tidy up the not commonly used to some httpservletrequest method, these we actually should know.

OK, when we use the MyEclipse new JSP page, the top 2 lines bring out the message saying:

<%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";%>

The path here is the beginning of the absolute path we write when we submit the form, which is the project name.

BasePath is the complete path to the entire submission request.


I write a servlet below, the inside call these methods, unified do a collation, although not how to use, but still need to know these methods.

Package Linkin;import Java.io.ioexception;import Java.util.enumeration;import javax.servlet.ServletException; Import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Linkinservlet extends httpservlet{private static final long Serialversionuid = 1L; @Overrideprotected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, ioexception{//the context path of the request/linkinsystem.out.println (Req.getcontextpath ());//Request Complete uri/linkin/ LinkinServletSystem.out.println (Req.getrequesturi ());//The full URL of the request http://localhost:8080/linkin/ LinkinServletSystem.out.println (Req.getrequesturl ());//The path of the requested servlet in the project/LINKINSERVLETSYSTEM.OUT.PRINTLN ( Req.getservletpath ());//The requested HTTP request type GETSYSTEM.OUT.PRINTLN (Req.getmethod ());//The Requested protocol type HTTPSYSTEM.OUT.PRINTLN ( Req.getscheme ())///Requested host IPLOCALHOSTSYSTEM.OUT.PRINTLN (Req.getservername ());//Port of the requested host 8080SYSTEM.OUT.PRINTLN ( Req.getserverport ());//The information inside the header of the request, here is the Get method, so it's all nUllenumeration enumeration = Req.getheadernames (); while (Enumeration.hasmoreelements ()) {String header = (string) Enumeration.nextelement (); System.out.println (header + ":" + req.getheader ("header"));/* host:nullconnection:nullcache-control:nullaccept:nulluser-agent:nullaccept-encoding:nullaccept-language:null*/} } @Overrideprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {This.doget (req, resp);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Servlet--httpservletrequest some ways about paths

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.