In my Spring(3.1) MVC web application(servlet 3.0) i have following href link in one of the JSP-s:
<a href="./edit_account?id=${account.accountId}">
<i class="icon-th-list"></i>${account.accountId} ${program.customer} </a>
It used to work properly whenever user clicks on above link on the web page, edit_account used to get appended to application url and used to reach corresponding method within Controller class.
Now due to security reasons, I had to make all cookies secure and http-only. Hence added following snippet in web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<session-config>
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
</cookie-config>
</session-config>
</web-app>
After adding above, none of the clicks(href in all JSP-s) working.
Following Error is thrown at web page, however j_session_id is showing up on the Url.
Http 400 description The request sent by the client was syntactically incorrect.
in all my JSP-s, session is true.
Can somebody please help me what changes I need to do make so that all the flows working even after adding above snippet in web.xml?
If you enable secure flag, this means your browser is not allowed to send jsessionId over cookies in the request header back to your server. Therefore IF you try to connect your server over HTTP (instead of HTTPS) the jsessionId cannot be sent over cookies in the http request and must be included into your url links so that the servlet can somehow track the users session.
To solve this isseu, you have to use HTTPS by default.
P.S: Actually links with jsessionId shouldn't be a problem. Please check the URL syntax it should be like this http://www.example.org/some_path;jessionid=d398jdsjnck398.1?paramater_name=parameter_value . If the ;jessionid is always at the end of the URL then it may be an indicator that the links are incorrectly generated. A possible reason for that may be that misuse of tag.
Related
Hello I am a newbie and I am new at JEE.
I try to connect my servlet class to the web.xml file but I always have this error:
Servlet should have a mapping name
and I don't know why and what is the purpose of adding a mapping name
Here is my web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SelectLiquorServlet</servlet-name>
<servlet-class>com.sample.SelectLiquorServlet</servlet-class>
</servlet>
First : Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets.
See first bullet of step # 2 on Wikipedia page, Java Servlet. (Emphasis added)
The following is a typical user scenario of these methods.
Assume that a user requests to visit a URL.
The browser then generates an HTTP request for this URL.
This request is then sent to the appropriate server.
The HTTP request is received by the web server and forwarded to the servlet container.
The container maps this request to a particular servlet. ⬅
The servlet is dynamically retrieved and loaded into the address space of the container.
The container invokes the init() method of the servlet.
…
To solve your problem you need to add the following lines:
<servlet-mapping>
<servlet-name>SelectLiquorServlet</servlet-name>
<url-pattern>/SelectLiquor</url-pattern>
</servlet-mapping>
Happy Coding
I would like to set the welcome-file of my JSP/JavaBeans project. I have a servlet named 'Controller.java' with the following #WebServlet annotation:
#WebServlet(name="Controller", urlPatterns={"/login", "/show_dbs"})
and I hava a web.xml file with the following content:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>Controller</welcome-file>
</welcome-file-list>
</web-app>
Almost all things are going well, I can open http://localhost:8080/PROJECT/login and http://localhost:8080/PROJECT/show_dbs and I come to Controller.java. But when I open http://localhost:8080/PROJECT/ I get a 404 error.
I'm using Eclipse with a 'Dynamic Web Project', the Controller.java file is located under /src (default package) and the web.xml file is under /WebContent/WEB-INF.
I hope you have a tip for me.
Thank you for your help. Here comes my solution:
If you want to set your servlet as welcome file you have to do the following:
Define a standard html as welcome-file such as index.html in your web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Make sure this file (index.html) doesn't exist.
Define your urlPatterns in #WebServlet like this:
#WebServlet(name="Controller", urlPatterns={"/index.html", "/login", "/show_dbs"})
Now every request to http://.../PROJECT/ (root) will be redirected to http://.../PROJECT/index.html and this calls the servlet.
In the welcome file list you must specify the URIs. But you have specified the name of the servlet.
Quote from the Java™ Servlet Specification version 3.0 (emphasis mine):
10.10 Welcome Files
Web Application developers can define an
ordered list of partial URIs called welcome files in the Web
application deployment descriptor. The deployment descriptor syntax
for the list is described in the Web application deployment descriptor
schema.
The purpose of this mechanism is to allow the deployer to
specify an ordered list of partial URIs for the container to use for
appending to URIs when there is a request for a URI that corresponds
to a directory entry in the WAR not mapped to a Web component. This
kind of request is known as a valid partial request.
The use for this facility is made clear by the following common example: A welcome file
of 'index.html' can be defined so that a request to a URL like
host:port/webapp/directory/, where 'directory' is an entry in the WAR
that is not mapped to a servlet or JSP page, is returned to the client
as 'host:port/webapp/directory/index.html'.
If a Web container receives a valid partial request, the Web container must examine the
welcome file list defined in the deployment descriptor. The welcome
file list is an ordered list of partial URLs with no trailing or
leading /. The Web server must append each welcome file in the order
specified in the deployment descriptor to the partial request and
check whether a static resource in the WAR is mapped to that request
URI. If no match is found, the Web server MUST again append each
welcome file in the order specified in the deployment descriptor to
the partial request and check if a servlet is mapped to that request
URI. The Web container must send the request to the first resource in
the WAR that matches. The container may send the request to the
welcome resource with a forward, a redirect, or a container specific
mechanism that is indistinguishable from a direct request.
If no matching welcome file is found in the manner described, the container
may handle the request in a manner it finds suitable. For some configurations this may
mean returning a directory listing or for others returning a 404 response.
P.S.
Also see the examples in the specification in the chapter 10.10
This is what your web.xml should be. Create an index.jsp. Use the index.jsp as your welcome file. The controller class is your sevlet. So define a servlet in your web.xml as follows. This should cause all requests to be directed to the Controller class. And you should still be able to access localhost:8080/PROJECT/, in which case it will be directed to your welcome file.
If you don't want to create index.jsp, you can use your existing jsp file, may be your login.jsp file as your welcome file. In which case, just replace the index.jsp with login.jsp
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.company.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
</web-app>
You can specify the url "/Controller" in urlPatterns in #WebServlet annotation. I think it will work.
The default welcome file is index.html, so just add the URL pattern.
#WebServlet(name="Controller", urlPatterns={"/index.html","/login", "/show_dbs"})
Can I make the welcome-file of the website to be a servlet ? If yes , how ? I was trying something like :
<welcome-file-list>
<welcome-file>FilterForwarded</welcome-file>
</welcome-file-list>
<!-- FilterForwarded is a servlet -->
While deploying I do not see any error but when I try to open the website abc.com I get a message from the browser that it is unable to connect to this website.Why is it so ?
I want when anyone visits the website,I should be able to store the client's public IP. To do this I wrote a Filter which after taking the IP , passed it to the servlet (from there I could update the logs). After storing the IP , client be automatically redirected to index.jsp. Is there any way to achieve this ?
EDIT :
<servlet-mapping>
<servlet-name>FilterForwarded</servlet-name>
<url-pattern>/FilterForwarded</url-pattern>
</servlet-mapping>
This is the mapping defined in web.xml . When I use /FilterForwarded in welcome-file I get this message when I try to deploy : Bad configuration: Welcome files must be relative paths: /FilterForwarded
From the logs :
com.google.apphosting.utils.config.AppEngineConfigException: Welcome files must be relative paths: /FilterForwarded
at com.google.apphosting.utils.config.WebXml.validate(WebXml.java:125)
at com.google.appengine.tools.admin.Application.<init>(Application.java:150)
at com.google.appengine.tools.admin.Application.readApplication(Application.java:225)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:145)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:69)
at com.google.appengine.tools.admin.AppCfg.main(AppCfg.java:65)
If you map the filter to /* you should be able to intercept all requests and then log the IP from there.
Or is your requirement to only log Client IP for the landing page?
If so, you could change the default servlet for the Servlet container, but bear in mind this will change the default servlet for all requests that do not match mappings in your web.xml.
<servlet-mapping>
<servlet-name>FilterForwarded</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
A more complex, but potentially better solution, is to front your Java web container with a web server and use rewrite rules to proxy to your backend Servlets. This way will mean that you can control the Servlet that is accessed for your landing page without overriding the default servlet for all non-matching requests. This might be overkill for your problem though.
Following an issue reported on this question, a solution was found:
req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
This seems a bit strange and is not really 'portable' code (it won't hurt, but...). It seems specific to Tomcat 7. I am using Tomcat 7.0.14 as delivered by NetBeans 7.0.1.
I could not find documentation indicating it is necessary to enable async request processing in servlet 3.0 with a catalina attribute. I could not find documentation indicating something special was necessary at the Tomcat configuration level too.
Is there a way to avoid having to set ASYNC_SUPPORTED=true in each request to enable async servlet 3.0 processing in Tomcat 7?
A couple of things to check first:
Make sure any filters that operate on the request also support async (as addressed in one of the answers to the question you referenced).
Make sure you're using a Servlet 3.0 web.xml - for example:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
Try upgrading.
Bug 53623 fixed in 7.0.30.
"Enable remaining valves for Servlet 3 asynchronous processing support." (fixed in 7.0.16)
Check the Tomcat 7 ChangeLog for complete details.
Also, if you want to use async, then you'll need to make sure that all of the filters and valves in the chain (as well as the servlet, of course) all support async. This is likely the problem in the original question, as well as with your case, here.
I found that org.apache.catalina.ASYNC_SUPPORTED=true is only needed when you from one normal-servlet/jsp (internally) forward to an async-servlet!
Example: In my index.jsp, I embed <jsp:forward page="/path/AsyncServlet" />
I promise the AsyncServlet works fine on both Tomcat7 and Glassfish3, when I directly trigger it from browser!
However when I trigger it by index.jsp:
Tomcat7 reports 500 for "Not supported"
Glassfish3 reports 500 for "Request is within the scope of a filter or servlet that does not support asynchronous operations"
If I embed <% request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true); %> before <jsp:forward> in index.jsp, Tomcat7 goes OK, but Glassfish3 still is BAD!
So I found a solution for both Tomcat7 and Glassfish3 (without SYNC_SUPPORTED!):
Just EXACTLY attach followings in web.xml:
<servlet>
<servlet-name>indexPage</servlet-name>
<jsp-file>/index.jsp</jsp-file>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>indexPage</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
Conclusion (for me):
You can NOT forward from a normal-servlet/jsp/filter to an async-one! Since the async-request feature MUST be preset!
So the common solution for a servlet/jsp/filter which needs to forward to an async-servlet is:
Use <servlet>/<async-supported>true or #WebServlet(asyncSupported = true) for a pre-processed servlet;
Use <servlet>/<async-supported>true for a pre-processed jsp
Use <filter>/<async-supported>true or #WebFilter(asyncSupported = true) for a pre-processed filter;
Hope this may help a little bit!
My GAE java based application uses only one google user - the admin. For the admin web pages I generate the logout url using
UserServiceFactory.getUserService().createLogoutURL("/")
The generated url is always having a /zero at the end and clicking on it gives 'Error 404 NOT_FOUND'.
I The problem occurs on development server as well as the cloud. On dev server, this generated url is always looking like - http://localhost:8080/myapp/myurl/0 and when actually deployed on cloud it is similar http://myapp.appspot.com/myapp/myurl/0
I wonder why logout url generated is not working, is it something I am doing wrong or missing some configuration ? please help.
Check your web.xml. You have to add following section.
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
You can replace index.jsp with your choice.
Edit
I don't know what is wrong with your app. Here is a test app i have created.
http://rqtest123.appspot.com/
My web.xml look like
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
I think you shoul check your web.xml again.
Finally found it !!!
Earlier, through my spring controller I was passing the created logout url as
model.put("logout-url", UserServiceFactory.getUserService().createLogoutURL("/"));
And my JSP code looked like -
<a class="link" href="${logout-url}">Logout</a>
The variable name logout-url was the problem. Replaced it with logoutUrl and everything worked fine ! The - is not allowed in variable name.