How do I remove ending "/" from URL path in a Servlet? - java

I want to map my RPC service to http://path.com/RPC2 rather than /RPC2/
Inside my web.xml file, I currently have the url-pattern set to /
<servlet-mapping>
<servlet-name>RPC2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I tried to merely remove the url-pattern, but this didn't work. When I remove the url-pattern entry, Tomcat won't deploy it and Jetty works but at ../RPC2/
Here's the full XML file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
<servlet>
<description>Automatos RPC Server</description>
<servlet-name>RPC2</servlet-name>
<servlet-class>RPCServlet</servlet-class>
<init-param>
<param-name>streamMessages</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<!-- Optional! Defaults to text/xml and ISO-8859-1 -->
<param-name>contentType</param-name>
<param-value>text/xml; charset=ISO-8859-1</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RPC2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

Deploy your webapp on context root (in Tomcat, just rename the WAR to ROOT.war or set <Context path=""> instead of <Context path="/RPC2">). This way your webapp will be deployed to http://path.com. This way you can map the servlet on an URL pattern of /RPC2 and the servletcontainer won't auto-redirect to the webapp root / anymore.

Related

Specifying UI class for SpringVaadinServlet inside web.xml

I have a Vaadin application which uses ru.xpoft.vaadin.SpringVaadinServlet as its single servlet. Now I am trying to change the servlet class to com.vaadin.spring.server.SpringVaadinServlet.
But I don't know how to specify the UI class for com.vaadin.spring.server.SpringVaadinServlet inside web.xml. I know it is possible to specify the servlet and UI class without a web.xml(Using #WebServlet and #VaadinServletConfiguration annotations). But still I require to configure the servlet using web.xml.
Please help.
You can spedify servlet and UI like this in the web.xml (from the Vaadin 7 Documentation):
<?xml version="1.0" encoding="UTF-8"?>
<web-app
id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>
com.vaadin.server.VaadinServlet
</servlet-class>
<init-param>
<param-name>UI</param-name>
<param-value>com.ex.myprj.MyUI</param-value>
</init-param>
<!-- If not using the default widget set-->
<init-param>
<param-name>widgetset</param-name>
<param-value>com.ex.myprj.AppWidgetSet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
See also the Vaadin docs about Deploying an Application.

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

How to publish java web project in tomcat

To publish one java-web application in tomcat, I copy the project that named 'demo-mvc' to the tomcat's webapps folder.Then I visit "http://localhost:8080/demo-mvc/xx.jsp" in the chorme browser after starting tomcat but it prompts "The requested resource is not available".I tried editing the server.xml as follows
<Context docBase="D:\apache-tomcat-7.0.57\webapps\demo-mvc" path="/demo-mvc" reloadable="true" source="org.eclipse.jst.jee.server:website"/>
Finally it still does no effect.I am confused about where the problem is.
Try to clean your project from Project clean option and make sure you are mapping all your resources
In your web.xml the code should be like this:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Couldn't access index.html in my WebContent folder

I have tried to find a solution searching on Google, but did not succeed.
This is the structure of my application's folder.
<PROJECT_NAME>
--->WebContent
--->index.html
--->WEB-INF
--->META-INF
I deployed my application in Tomcat server.
My web.xml config file is the following:
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>fp</display-name>
<servlet>
<servlet-name>FpServlet</servlet-name>
<servlet-class>com.fp.FpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FpServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
I try to access localhost:8080/project_name/,and everything works fine, but when I try to access the index.html file like this localhost:8080/project_name/index.html, it doesn't show the index.html content. It shows my root project content. (localhost:8080/project_name/)
How can I get the index file content posted in the browser?
Any help will be very appreciated.
Please note that the folder is named "WEB-INF", not "WEB.INF".
You can use default servlet to serve your static resources. Here is how this can be used with tomcat:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

Tomcat shows 404

I've got a Java EE project that works ok in Eclipse. But when I deploy it to a server with a normal Tomcat-7 it shows a 404 error on every servlet page (while .jsp works).
I've tried to deploy there a .war file and tried to just copy the whole project folder including the WEB-INF directory. The result is the same.
web.xml file contains the following:
<?xml version="1.0" encoding="ASCII"?>
<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">
<display-name>acs</display-name>
<servlet>
<display-name>jsp</display-name>
<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>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.gif</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.svg</url-pattern>
<url-pattern>*.kml</url-pattern>
</servlet-mapping>
</web-app>
The every servlet class contains annotations like that
#WebServlet("/full")
So why it's happening? And is there a way to know where Tomcat trying to search that servlets? Or something. So now I'm like standing before the wall and it shows me 404 and I'm do not have a tiny idea what's going wrong...
For every servlet we are adding 2 main tags < servlet > and < servlet-mapping > like
<servlet>
<display-name>jsp</display-name>
<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>
</servlet-mapping>
While for "default" servlet I have not found the < servlet > tag with servlet-class mapping

Categories