在servlet中传递和检索路径参数

时间:2016-04-19 02:06:56

标签: jsp servlets path-parameter

我知道如何将参数作为mydomain.com?id=2请求传递,但不是?id=2方法,而是希望实现基于正斜杠的传递方法JSP Servlets中的参数?

与Stackoverflow类似:

stackoverflow.com/questions/20425024533/question-title-goes-here

这就是我目前所拥有的:

JSP Page 1

<html>
    <head>
    </head>
  <body>
    <!--Other Page HTML-->
    <a href="PostServlet">Click here to view Post</a>
  </body>
</html>

PostServlet

public class PostServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String post_id = request.getParameter("id");

        RequestDispatcher rd = getServletContext().getRequestDispatcher("/BlogPost.jsp");  
        rd.forward(request, response);

    }
}

的web.xml

  <servlet>
    <servlet-name>PostServlet</servlet-name>
    <servlet-class>com.myweb.web.servlet.PostServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>PostServlet</servlet-name>
   <url-pattern>/PostServlet</url-pattern>
  </servlet-mapping>

以Stackoverflow格式为例,您能告诉我如何以mydomain.com/questions/id_goes_here/post_title_goes_here的格式传递值(id)。

谢谢你们!

0 个答案:

没有答案