I've read several docs and I don't get it: I know I'm doing something wrong but I don't understand what. I've got a website that is entirely dynamically generated: there's hardly any static content at all.
So, trying to understand JSP/Servlet, I've written my own "front controller" intercepting every single query, it looks like this:
<servlet-mapping>
<servlet-name>defaultservlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Basically I want any user request, like:
example.org
example.org/bar
example.org/foo.html
to all go through a default servlet which I've written.
The servlet then examines the URI and find to which .jsp the request must be dispatched, and then does, after having set all the attributes correctly, a:
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp");
dispatcher.forward(req, resp);
When I'm using a url-pattern (in web.xml) like, say, *.html, everything works fine. But when I change it to /* (to really intercept everything), I enter an endless loop and it ends up with a... StackOverflow :)
When the request is dispatched, is the URI ".../WEB-INF/jsp/index.jsp" itself matched by the web.xml filter /* that I set?
EDIT apparently, no, because this is an exact mapping to index.jsp and hence it bypasses the web.xml url-pattern. So I still don't get how I can enter that endless loop.
How should I do if I want to intercept everything using a /* url-pattern and yet be able to dispatch/forward/?
I'm not asking about specs/Javadocs here: I'm really confused about the bigger picture and I'd need some explanation as to what could be going on.
Am I not supposed to intercept really everything?
If I can intercept everything, what should I be aware of regarding forwarding/dispatching?
Unfortunately, Serlvet spec doesn't allow to create a servlet mapping to match only incoming request, not forwards. However, this can be done for filter mappings (and by default filter mappings match only incoming requests).
So, the typical solution for intercepting everything with a single servlet is to use a UrlRewriteFilter:
<filter>
<filter-name>urlRewrite</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>urlRewrite</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>application</servlet-name>
<servlet-class>...</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>application</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
/WEB-INF/urlrewrite.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
"http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite default-match-type="wildcard">
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
</urlrewrite>
This way also allows you to specify exceptions from /* mapping for static files.
Try using struts in which front controller pattern is inbuilt. There you will have a action class and you can define forwards in struts-config file using which you can easily manage the forwards.
Go through the tutorial http://www.roseindia.net/struts/struts2/index.shtml. Hope this helps you.
Related
I am working on a simple java serverlet. It seems as if the server is not getting the parameters. This is my doPost() code.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String a = request.getParameter("a");
String b = request.getParameter("b");
System.out.println("I started doing something " + a + " " + b);
}
This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>AutoPresServerlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Main</display-name>
<servlet-name>Main</servlet-name>
<servlet-class>com.altechwebdesign.java.autopres.Main</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/Main</url-pattern>
</servlet-mapping>
</web-app>
I am using the Advanced Rest Client for Chrome to send the POST Requests.
Nothing in the evidence you have shown use explains why the parameters are null.
It seems as if the server is not getting the parameters.
I assume that means that you have evidence that your doPost method is actually being called. That points to the fact that the web.xml is correctly directing the request to your servlet code.
That implies that the reason the parameters are not showing up is that they have not been provided (correctly). But that is happening in the client code and/or the request that it is sending, and you haven't shown that to us.
(It is also possible that you don't have any evidence and your "if seems ..." is just a guess. If that is the case, you need to FIND OUT what is going on, and/or show us the evidence that you actually have.)
I also don't see why you have mentioned JSPs here. You haven't shown us any JSP code, and it is unlikely that it will be directly relevant to your problem.
you need a form or jsp page to carry html input type values and trigger a servlet.
then only your servlet can get the value
i would suggest to write form code in index.jsp and call action /Main from the form.
flow should be like
index.jsp->submitform->main servlet
Servlet can get parameter in your code if and only if your action is triggered with the following data(no matter Get or Post)
a=test_for_a&b=test_for_b
you can add some input/select/textarea with name(which would be the parameter name you use in servlet,namely a and b).
Well, ajax would be another solution, which is out of our scope, I bet.
you can test if the request & response working correctly by :
PrintWriter writer=response.getWriter();
writer.write("This is my post"); ,
you must have a html form for submitting a & b values. check there action and methods are correct,or restart the server and try again..
I am using Jetty. My default servlet is making a simple forward to an HTML file in my WEB-INF folder that is causing a java.lang.StackOverFlowError error. The error is fixed if I rename the file I am forwarding from a .html to .jsp
DefaultServlet.java
public class DefaultServlet extends HttpServlet{
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
req.getRequestDispatcher("WEB-INF/home.html").forward(req, resp);
}
}
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Default</servlet-name>
<servlet-class>DefaultServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
My guess is that instead of inserting the html content in the response body, the forward is sending the browser a redirect to /WEB-INF/home.html. This again calls the DefaultServlet and gets into an infinity loop. How can I prevent this?
Thanks.
The "default servlet", which is mapped on a special URL pattern of /, is a very special servlet which is invoked when there's a request which does not match any of the servlets mapped on a more specific URL pattern such as *.jsp, /foo/*, etc.
When you forward to home.html, for which apparently no one servlet is registered, then the default servlet is invoked once again. However, the default servlet is ignorantly forwarding to the very same HTML file once again instead of actually serving the requested HTML file. It'll on the forward still find no one servlet matching the forward URL and it'll still invoke the default servlet once again. And again. Etc. When this is performed so many times that the stack cannot keep track anymore of all those in sequence invoked doGet() methods (usually around 1000), then you'll get a StackOverflowError.
That it works with a JSP file has actually a very simple reason: there's already a JspServlet registered on an URL pattern of *.jsp. So the badly designed default servlet isn't invoked.
Your default servlet should instead be obtaining the HTML file's contents via ServletContext#getResourceAsStream() and write it to the HttpServletResponse#getOutputStream().
However, it's also quite possible that you completely misunderstood the whole meaning of "default servlet" and/or the special meaning of the URL pattern / and actually merely want a servlet acting as home page. In that case, you should be mapping the servlet on a more specific URL pattern (and please rename the currently obviously quite confusing class name DefaultServlet to something else):
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>com.example.HomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
And then register exactly that URL as welcome file:
<welcome-file-list>
<welcome-file>home</welcome-file>
</welcome-file-list>
You need kind of exclude urls ends with "html".
See for example this link explaining similar problem solution Can I exclude some concrete urls from <url-pattern> inside <filter-mapping>?
I've created servlet named MainContent. and I have such mapping
<servlet>
<display-name>MainContent</display-name>
<servlet-name>MainContent</servlet-name>
<servlet-class>ge.test.servlet.MainContent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainContent</servlet-name>
<url-pattern>/main</url-pattern>
</servlet-mapping>
so, when I go to the link:
//localhost:8080/MyAppl/main I enter into the servlets doGet() method. Then I create RequestDispatcher forward to the index.jsp.
everything works!
RequestDispatcher rd = context.getRequestDispatcher("/index.jsp?language="+ lang);
rd.forward(request, response);
everything works!
Question:
Now I need to change url-pattern. I need something like that-:when I enter to the localhost:8080/MyAppl/ I need to be redirected to my servlet.
So I create something like that:
<url-pattern>/</url-pattern>
ok, it works! I'm redirected to the servlet. but something wrong happend here. when Servlet created RequestDispatcher forward , there was no images and css in my index.jsp.
when I see in the firebug console, I've seen that errors:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8080/MyApp/font/font_big.css". localhost/:15
Resource interpreted as Image but transferred with MIME type text/html: "http://localhost:8080/MyApp/IMG/company.gif".
How can I fix that?
Yes, like #DwB pointed, '/' context is problematic URL pattern and it causes your problem.
Use
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
instead. It is "the servlet 3.0 way" to do this.
Sources
[1] http://www.coderanch.com/t/366340/Servlets/java/servlet-mapping-url-pattern
[2] How can I map a "root" Servlet so that other scripts are still runnable?
I have an action bean named HomeActionBean, which Stripes has bound to the URL "Home.action". The URL binding works. Now I would like to use "Home.action" as my welcome file, like this:
<welcome-file-list>
<welcome-file>Home.action</welcome-file>
</welcome-file-list>
This does not work with the default configuration from the Stripes quickstart guide. I get the error
A request made it through to some part of Stripes without being wrapped in a StripesRequestWrapper. [...]
I figured out that I can make it work if I add the line
<dispatcher>FORWARD</dispatcher>
to my web.xml like this:
<filter-mapping>
<filter-name>StripesFilter</filter-name>
<servlet-name>StripesDispatcher</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
My question is: Is this the appropriate way of using a Stripes action as a welcome file? Does adding that line with "FORWARD" in it to my web.xml have other, possibly unwanted consequences?
To bind an action bean to the homepage URL you should use: #UrlBinding("/") like this:
#UrlBinding("/")
public class HomepageAction {
// .. action bean code..
}
I want to create a JSP page or servlet that will work in 2 ways.
A user visits their own profile page:
http//something.com/profile/
Or they visit their friends page:
http://something.com/profile/FriendsName
They could also visit their own page through an explicit URL like:
http://something.com/profile/YourName
I have a servlet-mapping setup as follows to map any requests to /profile to my JSP that will handle that request.
<servlet>
<servlet-name>Profile</servlet-name>
<jsp-file>/profile.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Profile</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>
Then I was thinking I could setup a filter that will parse the HTTPServletRequest's URL to read after the /profile/.
<filter>
<filter-name>profile-filter</filter-name>
<filter-class>ProfileFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>profile-filter</filter-name>
<url-pattern>/profile*</url-pattern>
</filter-mapping>
Now the filter could set attributes in the HttpServletRequest, how would i go about pulling those out in the JSP page to check if a user name was specified and check if your own name was set?
How would I go about creating a page scoped bean in the ServletFilter so I could use in the JSP page using jspBean like this:
<jsp:useBean id="profileInfo" scope="page" class="ProfileInfo" />
I think a filter only serves to artificially break the logic apart. You can very easily determine the value of the portion of the URL that doesn't match the url-pattern described in the web.xml. The HttpServletRequest class has a method that returns this value for you, it's called getPathInfo(). Here's an example:
<%
String path = request.getPathInfo();
if (path == null || "".equalsIgnoreCase(path)) {
// The path was empty, display the current user's profile
} else {
// Display the named profile
}
%>
This doesn't help you at all with the request beans, but I think it helps with the design.
I'm not completely following the question, but if you want a jsp to access request parameters or attributes, just do:
<%
String parameter = request.getParameter("parameter");
String attribute = request.getAttribute("attribute");
%>
You can also get access to the request url, etc... if you need to do anything with those.
Personally, I'd recommend that you use a servlet to handle your requests, and forward to the jsp of your choosing (possibly after setting session information that the jsp can use).
Using server level filters and request attributes for this kind of thing may be a bit overkill, but only you know your project's true requirements.