How to map jsp class in web.xml? - java

I am trying to map my compiled jsp class which is available in org.apache.jsp folder in tomcat server folder to the web.xml file so that I don't want to ship my jsp code.
I am using following code, but getting HTTP Status 404 -. I cross checked, paths are correct and class files are also available in that path I don't know why i am getting this error.
<?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">
<servlet>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<servlet-class>org.apache.jsp.index_jsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
</web-app>
Can any one help me to fix this?

you must use jsp-file tag for jsp mapping in web.xml
<?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">
<servlet>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<jsp-file>org.apache.jsp.index_jsp.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<url-pattern>/index_jsp.jsp</url-pattern>
</servlet-mapping>

Related

Unable to map servlet

I am starting with servlets and I am trying to get 2 numbers from a form and add them using java logic but when running it is showing The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. So I am guessing it is mapping problem can someone help?
Here is my web.xml code
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>Addition</display-name>
<servlet>
<servlet-name>addNum</servlet-name>
<servlet-class>addLogic.sum</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addNum</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
</web-app>
Also an image of project explorer for mapping

Where is the Invalid Content in Servlet Mapping

Getting the following erorr for invalid url pattern. Trying to follow this tutorial from Telusko exactly. https://www.youtube.com/watch?v=wty6OROO__8&list=PLsyeobzWxl7pUPF2xjjJiG4BKC9x_GY46&index=6
The action attribute in my form is set to "add" and I've tried with and without the forward slash without success. Please help. Thank you.
cvc-complex-type.2.4.a: Invalid content was found starting with element 'servlet-mapping'. One of '{"http://
xmlns.jcp.org/xml/ns/javaee":url-pattern}' is expected.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.centeno.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<servlet-mapping>/add</servlet-mapping>
</servlet-mapping>
</web-app>
Try url-pattern, not servlet-mapping inside tag servlet-mapping
<servlet-mapping>
<servlet-name></servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>

How to configure web.xml, glassfish-web.xml files for JSF?

I have simple project with no servlet but with a JavaBean class used in JavaServer Faces xhtml files.
How do i configure web.xml, glassfish-web.xml files? The whole project is managed by maven.
Here is the content of web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>LoginJSFApp</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>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
and glassfish-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<context-root>/LoginJSFApp</context-root>
</glassfish-web-app>
Your question is too broad. There are a lot of things you can add to web.xml : filters, servlet declarations, security stuff and many more. It depends on you each concrete case.
This is a very basic stuff that any web.xml should contain:
<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_3_1.xsd"
version="3.1">
// stuff here
</web-app>
Here is an example of web.xml with some stuff inside:
<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_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<display-name>HelloWorld Application</display-name>
<description>
This is a simple web application.
</description>
<!-- This is how you can add servlet -->
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>examples.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
This documentation contains a lot of information about what you can have inside web.xml and what for. I would suggest you check it.
Happy Coding :)
I have finally solved it. It appears that there were two problems:
xhtml files were in 'src/main/' folder instead of 'src/main/webapp/'
glassfish-web.xml had to be removed.

I want to call a Servlet as a very first file

I want to call a Servlet as a very first file to execute like welcome file.
In this servlet I am retrieving data from database and rendering it to display page at a very first page.
what I need is when I run program
either
url should be-http://localhost:8083/projectName/servletUrl
not http://localhost:8083/projectName/
or
if url is http://localhost:8083/projectName this should hit my servlet(/servletUrl) not welcome file.
Edit this file WebContent->WEB-INF->lib->web.xml.
It will only be visible if you have ticked the Generate web.xml deployment descriptor while creating the project.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
version="3.1">
<display-name>Database_Conn</display-name>
<welcome-file-list>
<welcome-file>ServletURLpattern</welcome-file>
</welcome-file-list>
</web-app>
Configure your servlet URLpattern as <welcome-file> in web.xml file located in WEB-INF folder of webapp like below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ProjectName</display-name>
<welcome-file-list>
<welcome-file>ServletURLpattern</welcome-file>
</welcome-file-list>
</web-app>
Assuming you use eclipse as IDE and servlet version 3 or 3.1 than you have to create web.xml manually.
I have used such servlet mapping:
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The key part is URL pattern that matches all possible URLs unless you add another servlet-mapping for other servlets.
<servlet>
<servlet-name>PenServlet</servlet-name>
<servlet-class>com.sun.PenServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
Here load-on-startup is an attribute of web.xml that will loaded first
if it has a lowest Integer Number.for example if you have 3 servlet that
is mentioned in the web.xml like
<servlet>
<servlet-name>PenServlet1</servlet-name>
<servlet-class>com.sun.PenServlet1</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>PenServlet0</servlet-name>
<servlet-class>com.sun.PenServlet0</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>PenServlet2</servlet-name>
<servlet-class>com.sun.PenServlet2</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
Here then load-on-startup 0 is loaded first in your web application
then 1 and 2 and so on..
you need to use this to get data and set it to your first page or return
your page from servlet with loaded data.

Servlet <url-pattern>: Trouble Mapping to a Directory that's Below My Application Directory

I want to have a dedicated directory for jsp and servlet development, but I can't map my app's logical name resource correctly. I tested the app without the extra directory as follow:
C:\tomcat1\webapps\beer-v1
The app ran successfully. But if I map the application to(my desired location):
C:\tomcat1\webapps\dev\beer-v1
The application does not run. I get a 404 error, resource not found. The following is my web.xml file.
<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"
version="3.0">
<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>

Categories