0

I know how to pass parameters as a mydomain.com?id=2 request, however instead of the ?id=2 approach I'd like to implement a forward slash based approach to passing parameters in JSP Servlets?

Similar to Stackoverflow:

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

This what I currently have:

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>

Taking Stackoverflow format as the example, can you show me how I could pass a value (id) in the format of mydomain.com/questions/id_goes_here/post_title_goes_here.

Thanks guys!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jay
  • 4,873
  • 7
  • 72
  • 137
  • Are you sure you want to build this from scratch? You need to implement some sort of Url mapping which parse the Url and dispatch it to corresponding controller. I suggest that you should use the framework, such as spring or java ee, to simplify the problem. – gigadot Apr 19 '16 at 02:15
  • Do you know what that format is called? The Forward slash based parameter? @gigadot And can you link me to a Spring or EE Guide please? – Jay Apr 19 '16 at 03:09
  • I'm not quite sure what you mean exactly. The format you are probably, talking about is called "part" of URL. See here https://en.wikipedia.org/wiki/Uniform_Resource_Locator . And the method of identifying resources using URL is called RESTful. For spring tutorial, http://projects.spring.io/spring-boot/#quick-start . You will notice that there are a lot of learning you have to do because what you were going to do would be huge and it has been done before. – gigadot Apr 19 '16 at 03:52
  • I am not an expert on Java EE so I cannot give you any suggestion. Since I learned about Spring framework, I never implement servlet myself ever again because it's a waste of time. – gigadot Apr 19 '16 at 03:55
  • 1
    Those things are called "path parameters". – BalusC Apr 19 '16 at 08:11

0 Answers0