I want to call a Servlet as a very first file - java

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.

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>

OpeningRestful API ends with 404 allt the time

I have a done MVC Java project and I need to make an API for it. I'm trying to use Jersey. I installed it according to this instruction:
http://www.mazong1123.com/use-jersey-2.0-without-maven/
Since my project was already done and it was not a maven. Also, I'm following this instruction: http://www.mazong1123.com/use-jersey-2.0-without-maven/
The thing is I've downloaded and put all.jar files into my project WEB-INF, then I created RESTful Web Services from Patterns (created class within my package where the model, servlet, and .jdbc are. After that BookResource.java and ApplicationConfig.java were created as on the screen:
and edited my web.xml to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" id="WebApp_ID"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<display-name>parking-space-booking-system</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>PBS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.pbs.web.jdbc</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PBS</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
I'm using Tomcat server. Whenever I provide following URL: http://localhost:8084/PBS/webapi/booksresource I'm getting 404. Is it something wrong with my web.xml?
Regards

404 Not Found error was encountered while trying to use an ErrorDocument to handle the request

I am developing a website and i am finding hosting issues. I have put all my .jsp files in the root directory.
I am able to see the JSPs (accessing directly to them).
But, when i try to perform the same check the WEB-INF/classes, I get the error:
Not Found The requested URL /AC/SearchController was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
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://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>AC</display-name>
<welcome-file-list>
<welcome-file>Home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AdvisorProfileServlet</servlet-name>
<servlet-class>org.AC.controller.AdvisorProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdvisorProfileServlet</servlet-name>
<url-pattern>/AC/AdvisorProfilePage?aId=*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ForgotPasswordRedirectController</servlet-name>
<servlet-class>org.AC.controller.ForgotPasswordURLController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForgotPasswordRedirectController</servlet-name>
<url-pattern>/ForgotPasswordAdvisor</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AdvisorMyAccountRequest</servlet-name>
<servlet-class>org.AC.controller.AdvisorMyAccountRequestController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdvisorMyAccountRequest</servlet-name>
<url-pattern>/AC/requests.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ForgotPasswordRedirectControllerUser</servlet-name>
<servlet-class>org.AC.controller.UserForgotPasswordURLController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForgotPasswordRedirectControllerUser</servlet-name>
<url-pattern>/ForgotPasswordUser</url-pattern>
</servlet-mapping>
</web-app>
This might also be a server config, because I created the project on Tomcat server 8, but I guess the server which is now hosting it is Tomcat 7.
How can I resolve this?
Please declare "/AC/SearchController" as url pattern in your web.xml

java web app- unable to see jsp file after adding content to web.xml for servlet context listener

I have added the following content to my web.xml for a servlet context listener---
<listener>
<listener-class>
com.anyaservices.log4j.ApplicationServletContextListener
</listener-class>
</listener>
I have added this immediately after the end of "welcome-file-list"
The last line of the welcome-file-list node is given below--
</welcome-file-list>
Now, I have made some changes to a servlet so that it can use the context listener... in addition to the servlet there is a "Hello.jsp" file that just displays a welcome message.
I have not made any changes to the JSP file. However after my code changes for the servlet context listener, when I try to go to Hello.jsp I get an error in Tomcat= The requested resource is not available.
What have I done wrong here? Why is the JSP file not being shown now?
EDIT-- I have posted the entire content of my web.xml below--
<?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" id="WebApp_ID" version="2.5">
<display-name>Test Web Application</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>Hello.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
com.test.log4j.ApplicationServletContextListener
</listener-class>
</listener>
<servlet>
<description>Used to run a single crawl job</description>
<display-name>runsinglecrawljob</display-name>
<servlet-name>runsinglecrawljob</servlet-name>
<servlet-class>com.test.runsinglecrawljob</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>runsinglecrawljob</servlet-name>
<url-pattern>/runsinglecrawljob</url-pattern>
</servlet-mapping>
<servlet>
<description>Home page</description>
<display-name>home</display-name>
<servlet-name>home</servlet-name>
<servlet-class>home</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet>
<description>Run a single crawl job after asking users for parameters of that job</description>
<display-name>runsinglejob</display-name>
<servlet-name>runsinglejob</servlet-name>
<servlet-class>com.test.runsinglecrawljob</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>runsinglejob</servlet-name>
<url-pattern>/runsinglejob</url-pattern>
</servlet-mapping>
</web-app>
The URL I am trying to access is http ://localhost:8080/test_web_app/Hello.jsp
Tomcat handles JSP files through org.apache.jasper.servlet.JspServlet. Perhaps somehow your listener class is interfering with the operation of the JspServlet. There is no need to list all of those files in your welcome file list. Any jsp file inside the war should be available (but it should not be placed inside the WEB-INF or META-INF folders.)
I think you should check your log4j configuration. Did you initialize log4j with a log4j.properties file in WEB-INF/classes?

Categories