Elaborating on this:
I map a servlet or filter to "/*"
Now, if I access a url like:
/test
Then this will be directed to the servlet (which is okay)
But if i access a url like:
/index.jsp
This will be directed also to the servlet, I dont want this behavior, what I want is for index.jsp to be processed as jsp.
How can this be done?
Map your controller servlet on a more specific url-pattern like /controller/* and create a Filter which is mapped on /* and does roughly like follows in doFilter() method.
String uri = ((HttpServletRequest) request).getRequestURI();
if (uri.endsWith(".jsp")) {
chain.doFilter(request, response); // Just let it go. The container's builtin JspServlet will pickup this.
} else {
request.getRequestDispatcher("/controller" + uri).forward(request, response); // Goes to controller servlet.
}
Related
I use getServletContext().getRequestDispatcher("/message.jsp").forward(request, response); to forward from servlet to jsp, but this does not work in server. I used response.sendRedirect(request.getContextPath() + "/message.jsp"); but then jsp does not show the message which I send from servlet. How to solve it?
If you do a sendRedirect instead of a forward, you will create a new request, which will cause your request attributes to be gone. Some frameworks use a 'flash' scope (essentially 2 times a request) instead of the request or session scope to handle this. In your case, however, there is no flash scope since you are using plain Servlets/JSP.
Instead, you can do something like:
ServletContext context = getServletContext().getContext(request.getContextPath());
RequestDispatcher dispatcher = context.getRequestDispatcher("/message.jsp");
dispatcher.forward(request, response);
Which should forward the current request to the message.jsp.
you can do by this way
request.setAttribute("PARAM1", "VALUE1");
RequestDispatcher dispatcher = request.getRequestDispatcher("message.jsp");
dispatcher.forward(request, response);
and in your Message.jsp retrieve your set value.
request.getAttribute("PARAM1");
I trying to do two things at once and I don't seem to find answers that can do both.
I'm trying this:
I want URLs ending in /user/{parameter} to be mapped to a JSP page user.jsp where I can access the parameter.
I find ways to pass parameters from the web.xml file to the JSP using this method
<init-param>
<param-name>someParam</param-name>
<param-value>itsValue</param-value>
</init-param>
And I find ways to create URL filters and map them to Java Servlets.
What I need is a combination. Also, what I found on passing URL parameters to Servlets wasn't too detailed either, so a good reference on that would also be more than welcome!
I want URLs ending in /user/{parameter} to be mapped to a JSP page user.jsp where I can access the parameter.
Map a servlet on /user/* and use HttpServletRequest#getPathInfo() to extract the path info.
E.g.
#WebServlet("/user/*")
public class UserServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pathInfo = request.getPathInfo(); // "/{parameter}"
// ...
request.getRequestDispatcher("/WEB-INF/user.jsp").forward(request, response);
}
}
That's basically all. Use the usual String methods like substring() and/or split() to break down the path info in usable parts.
Use a base-url servlet to parse the URL and perform a conditional servlet forwarding to the appropriate JSP.
request.getRequestDispatcher().forward(JSPurl)
Let us say you have a URL branch /sales/. Under this URL branch, you would allow the following URLs to to be serviced by /implemented/usersales.jsp :
/sales/liquour/users/{region}
/sales/liquour/soft/users/{region}
/sales/toiletries/users/{type}
and the following URLs to be serviced by /implemented/products.jsp
- /sales/groceries/products/{section}
- /sales/groceries/meat/products/{region}
- /sales/groceries/vegetables/beans/products/{region}
You would have a web.xml servlet mapping for servlet class org.myorg.SalesHandler to be mapped to /sales/.
In the service method override in org.myorg.SalesHandler servlet class,
analyse the URL pattern (using regexp or otherwise) and then conditionally forward the request using
request.getRequestDispatcher().forward(JSPurl)
JAX-RS
But why would you do that when we have jax-rs?
How to forward from a JAX-RS service to JSP?.
JAX-RS may seem daunting at first, but it is actually very simple and intuitive to implement.
I have two controller. One is main controller and other is intermediate controller. In intermediate controller I should add header to HTTPRequest. After Adding I should redirect it to main controller where I should check the header I added. How can I do this? Can anyone help me for this?pls........
Seems like a good place to use Servlet Filter . If you want to pre-process an incoming request you could do this with servlet filters and then simply chain it to the appropriate servlet .
Refer BalusC Answer for details .
If this is not the case your can merely forward the request like :
request.getRequestDispatcher("/yourServlet").forward(request, response); // forward to the main servlet
Forward Dispatching
getServletContext().getRequestDispatcher().forward("second page");
The request forwarding is done internally by the JSF Controller Servlet to another resource. The browser is unaware of what has happened in the server side at the web container. So it still thinks it is tending to the original request and displays the original URL in its address bar. However, the page content displayed is from the second page.
Redirect Dispatching
response.sendRedirect("second page");
In this case, the JSF Controller Servlet instructs the client browser (via HTTP response header) to fetch another URL. So the browser fetches entirely a new URL and displays the second URL in its address bar. This could cause slight performance delay
from here
I think you need to forward the request rather than redirecting.
RequestDispatcher dispatcher= request.getRequestDispatcher("servlet-mapped-url");
dispatcher.forward(request, response);
How do i specify a redirection to another servlet, in the doPost() method of a servlet.
at the moment I'm using
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
which is wrong since, my parameters in the doGet() method of products are not being called and initialized.
So I'm left with an empty products page after logging in :/
You need to use HttpServletResponse#sendRedirect() to send a redirect. Assuming that the servlet is mapped on an URL pattern of /products:
response.sendRedirect("/products");
This way the webbrowser will be instructed to fire a new HTTP GET request on the given URL and thus the doGet() method of the servlet instance will be called where you can in turn load the products and forward to a JSP which displays them the usual way.
In your doPost you can always call:
return doGet(request, response);
I am using
<url-pattern>/*</url-pattern>
to map all the requests to one sevlet,where i do all the authentication work.
but I want to skip some static content (like css files).so I tried fowrding them from
that sevlet to where the resource file is
if(isResourceFile){
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("static/" + fileName);
dispatcher.forward(request, response);
}
but this will start a infinite loop because this will again call the same sevlet
is there any way to work around this without mapping all the resource(css) files in web.xml?
The url-pattern of /* is better to be used exclusively by Filters, not Servlets. Put all your static files in a certain folder (maybe covered by the url-pattern of a more effective FileServlet?) and let the Filter check it.
E.g. (pseudo)
public void doFilter(request, response, chain) {
if (requestURI.startsWith("/static")) {
chain.doFilter(request, response); // Just continue. Do nothing.
} else {
request.getRequestDispatcher("/pages").forward(request, response); // Forward to page controller.
}
}
Hope this helps.
Assuming that you're looking to authenticate just JSP files, you could change the URL:
/*.jsp
I think it's a better idea to handle your authentication using a filter rather than a servlet. And in a production environment, you should use a front-end webserver for static content.
In case you cannot use a filter and you have to use a wildcard '*' in the mapping without a extension like '.jsp' then then this could work for you:
if(isResourceFile){
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/static/" + fileName);
dispatcher.forward(request, response);
return;
}
Adding the '/' in the beginning of the static directory will give it root context.