简体   繁体   English

如何在Java中的Servlet中读取URL和打印

[英]how to read URL and Print in Servlet in java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().append("Served at: ").append(request.getRequestURL());
    System.out.println("Received Value: "+request.getRequestURL());
    Utils.getDataFromFeedbackLink(request.getContextPath());        
    response.setContentType("text/html"); 
    PrintWriter pw=response.getWriter();      
    pw.println("<html><body>");  
    pw.println("Welcome to servlet"+request.getRequestURL());  
    pw.println("</body></html>");   
    pw.close();  

}

web.xml web.xml

 <servlet>
    <servlet-name>SubmitFeedbackServlet</servlet-name>
    <display-name>SubmitFeedbackServlet</display-name>
    <description></description>
    <servlet-class>com.techjini.tfs.servlets.SubmitFeedbackServlet</servlet-class>
  </servlet>
      <servlet-mapping>
        <servlet-name>SubmitFeedbackServlet</servlet-name>
        <url-pattern>/submitfeedback</url-pattern>
      </servlet-mapping>

this is my Code of servlet get method : i want to print what ever request URL will be Like http://localhost:8080/TechJiniFeedbackSystem/submitfeedback/Hello servlet 这是我的servlet get方法代码:我想打印任何请求的URL都将像http:// localhost:8080 / TechJiniFeedbackSystem / submitfeedback / Hello servlet

then this URL should Print in my Consle i have used getContextPath and getRequestURL but non of these printing http://localhost:8080/TechJiniFeedbackSystem/submitfeedback/Hello servlet 那么此URL应该在我的Consle中打印,我已经使用过getContextPath和getRequestURL,但是这些都不打印http:// localhost:8080 / TechJiniFeedbackSystem / submitfeedback / Hello servlet

please suggest me where am doing wrong or suggest me to solve this issue . 请建议我哪里做错了,或者建议我解决这个问题。

It's incredible easy: you must use getRequestURL from HttpServletRequest in your Servlet in doGet method 这非常容易:您必须在doGet方法的Servlet中使用HttpServletRequest的 getRequestURL

or for more info you can use this methods: getServletPath(), getContextPath and etc. 或获取更多信息,可以使用以下方法:getServletPath(),getContextPath等。

read more in specification: https://docs.oracle.com/cd/E17802_01/products/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/http/HttpServletRequestWrapper.html#getRequestURL() 在规范中阅读更多信息: https : //docs.oracle.com/cd/E17802_01/products/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/http/HttpServletRequestWrapper.html#getRequestURL()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM