How to change url pattern for index.jsp - java

How can I change the URL pattern for index.jsp. I want to change the URL http://localhost:8084/eclinical/index.jsp to http://localhost:8084/eclinical/home. Below is the web.xml file:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>home</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>about</servlet-name>
<jsp-file>/about.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>about</servlet-name>
<url-pattern>/about</url-pattern>
</servlet-mapping>

You can register a jsp as a servlet using the jsp-file tag in the servlet definition. You can thus provide a path mapping to your jsp in web.xml
<servlet>
<servlet-name>myIndexJsp</servlet-name>
<jsp-file>index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>myIndexJsp</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>

Related

allow login page without authentication in web.xml

I am trying to make the login.jsp page public without authentication.
here is my web.xml file :
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class
</servlet>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
what should I add?

Not able to change default starting page in netbeans

I create a web application in Netbeans, named test9. I have create various jsp files and servlets,etc in it.Now i want to change the starting page index.html to index.jsp so I created a new file index.jsp and deleted index.html but after running it, It is still trying to open index.html and displaying 404 error resource not found i even made changes in my xml file but still it is doing it.
Do i have to make some other changes ?
xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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_3_1.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>RequestLoggingFilter</filter-name>
<filter-class>bean.RequestLoggingFilter</filter-class>
</filter>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>bean.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>RequestLoggingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>bean.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>bean.LogoutServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DepartmentServlet</servlet-name>
<servlet-class>bean.DepartmentServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>C_userServlet</servlet-name>
<servlet-class>bean.C_userServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>statusServlet</servlet-name>
<servlet-class>bean.statusServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>FileServlet</servlet-name>
<servlet-class>bean.FileServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SearchServlet</servlet-name>
<servlet-class>bean.SearchServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>NoCacheFilter</servlet-name>
<servlet-class>bean.NoCacheFilter</servlet-class>
</servlet>
<servlet>
<servlet-name>forgotServlet</servlet-name>
<servlet-class>bean.forgotServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>passServlet</servlet-name>
<servlet-class>bean.passServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DepartmentServlet</servlet-name>
<url-pattern>/DepartmentServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>C_userServlet</servlet-name>
<url-pattern>/C_userServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>statusServlet</servlet-name>
<url-pattern>/statusServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FileServlet</servlet-name>
<url-pattern>/FileServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SearchServlet</servlet-name>
<url-pattern>/SearchServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>NoCacheFilter</servlet-name>
<url-pattern>/NoCacheFilter</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>forgotServlet</servlet-name>
<url-pattern>/forgotServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
In netBeans you can right click button on your project, then properties. A new pop up window will open, and on the left menu there will be a label call run. Click there and then in the "relative URL" you should put
/nameOfYourJspFile.jsp
and it works but i still do not know why
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
did not work.
I know that this is an old post but since i had the same issue and solved it i am posting my answer to help some others who might stuck in a situation like this.
What worked for me was to take the new jsp file out of the WEB-INF folder.

How does Java servlet handle a URL that ends with '/'?

Usually, these two requests:
localhost:8080/test
localhost:8080/test/
have no difference.
However, when I add a servlet-mapping config:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
then
localhost:8080/test/
Does not work, it would return 404 error, I don't understand that I had added a config like this:
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/rest-servlet.xml,
/WEB-INF/interceptor-servlet.xml,
/WEB-INF/controller-servlet.xml,
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
So, why would the request not go to this servlet, what is the magic in *.html? The others like *.txt, *.jpeg would not cause this problem.
remove
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
leave
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and let Spring distinguish between rest and html requests.

servlets: display image from outside the project dir

I am developing JAVA web project by using netbeans IDE, I want to display some images from a folder outside the project's directory, after uploading them by my servlets, here is my code:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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_3_1.xsd">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>views/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>controllers.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>index</servlet-name>
<jsp-file>/views/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>students</servlet-name>
<servlet-class>controllers.StudentController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>students</servlet-name>
<url-pattern>/students</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>books</servlet-name>
<servlet-class>controllers.BookController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>books</servlet-name>
<url-pattern>/books</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>admins</servlet-name>
<servlet-class>controllers.AdminController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>admins</servlet-name>
<url-pattern>/admins</url-pattern>
</servlet-mapping>
<context-param>
<param-name>uploads</param-name>
<param-value>C:/Users/mohammad/Documents/NetBeansProjects/uploads</param-value>
</context-param>
</web-app>
img in the jsp:
<img src="C:/Users/mohammad/Documents/NetBeansProjects/uploads/name.jpg"/>
servlet:
I am sending it inside a json object:
object.add("<img src=\""+getServletContext().getInitParameter("uploads")+"/"+book.getImage()+"\"/>");
I am sure that the image is exist in that folder, but it couldn't loaded, what is the problem here?
You should add filter on web.XML file of image extensions that actually specify that what kind of external resources can be passed through front controller.. Will u please post your web.xml
U may try this link Upload an image to a Path set in web.xml using primefaces

GAE 404 NOT_FOUND error

i am trying to deploy my app to GAE but i allways get 404 not found error on all pages(jsp) except for default. RESTful webservices working without problem. I am using netbeans 7.1.1 and gae 1.6.4.1
When I test it locally, everything works fine without any problem. But when I upload it do gae, allways only default page is available. I tryied almost everything.
I tryied to check whether the files are on GAE using appcfg.cmd. I downloaded my files from GAE and found out that noone is missing so the update process was probably successful.
I tryied to upload it with appcfg script instead of netbeans plugin but the process failed because of:
com.google.appengine.tools.admin.JspCompilationException: Failed to compile jsp files.
I cant simply make it working using appcfg script, searech internet for almost whole day and found no possible solution. Uploading with netbeans plugin worked without problem.
So what can be the reason to allways throw 404 on gae? Please help.
Thx very much. If you need any other info, i will be glad to post it here.
Ok,Here is my web.xml
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>facebookLogin</servlet-name>
<servlet-class>Login.FacebookLogin</servlet-class>
</servlet>
<servlet>
<servlet-name>PlanProcess</servlet-name>
<servlet-class>Input.PlanProcess</servlet-class>
</servlet>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>facebookLogin</servlet-name>
<url-pattern>/facebooklogin</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>private</servlet-name>
<jsp-file>private.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>private</servlet-name>
<url-pattern>/private</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>editPlan</servlet-name>
<jsp-file>editTrainingPlan.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>editPlan</servlet-name>
<url-pattern>/private/trainingplan/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>listplans</servlet-name>
<jsp-file>listTrainingPlans.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>listplans</servlet-name>
<url-pattern>/private/listplans</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>visualize</servlet-name>
<jsp-file>visualize.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>visualize</servlet-name>
<url-pattern>/private/visualize</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>graph</servlet-name>
<jsp-file>graph.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>graph</servlet-name>
<url-pattern>/graph/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>created</servlet-name>
<jsp-file>created.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>created</servlet-name>
<url-pattern>/created</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>login.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>newRecord</servlet-name>
<jsp-file>newRecord.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>newRecord</servlet-name>
<url-pattern>/private/newrecord</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>newPlan</servlet-name>
<jsp-file>newPlan.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>newPlan</servlet-name>
<url-pattern>/private/newplan</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>newExcercise</servlet-name>
<jsp-file>newExcercise.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>newExcercise</servlet-name>
<url-pattern>/private/newexcercise</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>Webservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>test.InsertToDb</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/testinsert</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>recordProcess</servlet-name>
<servlet-class>Input.RecordProcess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>recordProcess</servlet-name>
<url-pattern>/record_process</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>excerciseProcess</servlet-name>
<servlet-class>Input.ExcerciseProcess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>excerciseProcess</servlet-name>
<url-pattern>/excercise_process</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PlanProcess</servlet-name>
<url-pattern>/plan_process</url-pattern>
</servlet-mapping>
After hours of debugging and searching, i found out, that if you want to map jsp files
you have to use
<servlet>
<servlet-name>editPlan</servlet-name>
<jsp-file>**/**xxx.jsp</jsp-file>
</servlet>
instead of
<servlet>
<servlet-name>editPlan</servlet-name>
<jsp-file>xxx.jsp</jsp-file>
</servlet>
The '/' in path is essential, it will work on loaclhost but not deployed on gae. What a crap...
I never mapped JSPs to anything in web.xml. Why do you do that?

Categories