views:

77

answers:

5

How can I use Servlets to access the HTML uses of having JSP without having to have all my client-facing pages called *.jsp?
I would rather do this than using all the response.write() stuff because I think it is easier to read and maintain when it is all clean "HTML".
Is this is fair assesment?

EDIT: What I'm going for is having the Servlets output things to the screen without having to redirect to a .jsp file.
In this way, I could write all the JSP stuff, but when it comes time to display it, the page the URL the user sees is essentially, "http://blah.com/posts/post-id" which is the address of the servlet and not "http://blah.com/posts.jsp?pos=post-id".
But I would still write all presentation logic in an external .jsp.

A: 

I'm not entirely sure what you're asking here. You can ge servlets themselves to write HTML, but that's not clean at all.

An alternative is to get your servlets to create HTML via a templating engine, such as Velocity or Freemarker. The syntax in the templates may be cleaner for your particular application, if less fully featured.

Brian Agnew
A: 

Back in ancient times (think '98...) this was called a "Model 2 architecture": a servlet received the request, processed it, and handed the request over to a JSP page that handled the view.

See this article for one example of how this is done, or simply search for "JSP Model 2".

Edit: for that, you can use RequestDispatcher.include() instead of forward() as described in the previous article. The rest should still be applicable.

andri
A: 

If I understand correctly you want to hide *.jsp extension from user, right?

In that case when your Servlet redirects to a jsp page have it do this:

RequestDispatcher disp = request.getRequestDispatcher("hidden.jsp");
disp.forward(request,response);   

By using Request Dispatcher instead of redirect you "hide" your .jsp extension behind the servlet name. However in case your JSP page redirects to another JSP page this won't work. If you want the .jsp file to be visible use response.encodeURL or response.sendRedirect

Daniel Fath
+2  A: 

Just hide the JSP away in /WEB-INF folder so that noone can access it directly and create a servlet which forwards the request to this JSP file. Don't do a redirect, else you will see the new URL being reflected in the address bar. E.g.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String postId = request.getPathInfo();
    // Do your business thing here. Any results can be placed in request scope. E.g.
    request.setAttribute("post", post); // post is a bean containing information you'd like to display in JSP.
    // Then forward request to JSP file.
    request.getRequestDispatcher("/WEB-INF/posts.jsp").forward(request, response);
}

Map this servlet on an url-pattern of /posts/*.

In the /WEB-INF/posts.jsp make use of taglibs to control page flow and EL to access the data. E.g.

<h2>${post.title}</h2>
<p><fmt:formatDate value="${post.date}" type="date" /> - ${post.message}</p>

Finally just invoke the servlet by http://example.com/posts/postid. The /postid part will be available by HttpServletRequest#getPathInfo(). You need to parse the value yourself and do the business thing with it.

BalusC
A: 

I think you're looking for the Front Controller Pattern - this is the basis of "JSP Model 2" web apps (as @andri mentioned) and pretty much all the (hundreds?) of Java web frameworks.

Nate