0

As the title says, how can I redirect a page without a particular parameter appended to its URL? For example, I have a page "file.jsp" that is supposed to take a parameter from its dispatcher, say "fileid". So the URL is of the form "file.jsp?fileid=123". If someone requests the page (or types it in) "file.jsp", without the fileid, then the page should get redirected to the main page. Can I use Filters here?

Yusaku
  • 13
  • 2

2 Answers2

0

What if you request the fileid in your Servlet and you redirect the user if fileid==null ?

Something like:

Object fileid = request.getParameter("fileid");

if(fileid == null)
{
  ReuqestDispatcher dispatcher = request.getRequestDispatcher("whatever.jsp");
  dispatcher.forward(request, response);
}
SklogW
  • 839
  • 9
  • 22
0

Yes you can :-) . But the more common way to ensure that a JSP is called with its parameters is to hide it under WEB-INF folder and only forward to it from a servlet.

That way, if a user directly types file.jsp in its browser it will get a page not found error - that you can redirect to a specific page.

Another solution if you have a JSP only webapp (but you should not for a serious project ...) would be to put a test at the beginnning of the JSP that would forward or redirect to another page. Something like :

<c:if test="${empty param.fileid}" >
    <jsp:forward page = "/"/>
</c:if>

For an example showing how to do a redirect from a JSP just look at this other question

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252