getting tomcat error with servlet-mapping [duplicate] - servlets

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
Actually this is my first servlet application.just getting started..
below is my code:
package newpackage.org;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//#WebServlet(description = "a general servlet", urlPatterns = {
"/simpleservlet" })
public class simpleservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServletdoGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doget(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
PrintWriter obj= response.getWriter();;
// String username= request.getParameter("username");
obj.println("again getting back with ");
}
}`
XML
<?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"
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>myproject</display-name>
<servlet-mapping>
<servlet-name>xmlservlet</servlet-name>
<url-pattern>/002path</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>xmlservlet</servlet-name>
<servlet-class>newpackage.org.xmlservlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simpleservlet</servlet-name>
<url-pattern>/001path</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>simpleservlet</servlet-name>
<servlet-class>newpackage.org.xmlservlet </servlet-class>
</servlet>
</web-app>
ERROR:
HTTP Status 404 - /myproject/servlet/newpackage.org.simpleservlet
type Status report
message /myproject/servlet/newpackage.org.simpleservlet
description The requested resource is not available.
Apache Tomcat/7.0.72

You need to remove the below in web.xml
<servlet-mapping>
<servlet-name>simpleservlet</servlet-name>
<url-pattern>/001path</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>simpleservlet</servlet-name>
<servlet-class>newpackage.org.xmlservlet </servlet-class>
</servlet>
And use the below code in servlet:
#WebServlet(description = "a general servlet", urlPatterns = {"/simpleservlet", "/001path" })

<servlet-name>simpleservlet</servlet-name>
<servlet-class>newpackage.org.xmlservlet </servlet-class>
</servlet>
Give simpleservlet class name instead of xmlservlet
<servlet-name>simpleservlet</servlet-name>
<servlet-class>newpackage.org.simpleservlet </servlet-class>
</servlet>

Related

Servel init-param returns null

I'm trying to load some init-param values into a serevlet through the web.xml file but they keep showing up null. I do have two context-param's that work fine. Does anyone know what I'm doing wrong?
This is the web.xml file that I'am using for my application.
<?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_3_1.xsd"
version="3.1"
>
<display-name>Hello World Application</display-name>
<servlet>
<servlet-name>contextxParameterServlet</servlet-name>
<servlet-class>com.wrox.HelloServlet</servlet-class>
<init-param>
<param-name>database</param-name>
<param-value>CustomerSupport</param-value>
</init-param>
<init-param>
<param-name>server</param-name>
<param-value>10.0.12.5</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>contextxParameterServlet</servlet-name>
<url-pattern>/contextParameters</url-pattern>
</servlet-mapping>
<context-param>
<param-name>settingOne</param-name>
<param-value>foo</param-value>
</context-param>
<context-param>
<param-name>settingTwo</param-name>
<param-value>bar</param-value>
</context-param>
<servlet>
<servlet-name>servletParameterServlet</servlet-name>
<servlet-class>com.wrox.initParams</servlet-class>
<init-param>
<param-name>database</param-name>
<param-value>CustomerSupport</param-value>
</init-param>
<init-param>
<param-name>server</param-name>
<param-value>10.0.12.5</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletParameterServlet</servlet-name>
<url-pattern>/servletParameter</url-pattern>
</servlet-mapping>
</web-app>
This is page mapped to servletParameterServlet.
package com.wrox;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;
public class initParams extends HttpServlet{
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
ServletContext c = this.getServletContext();
PrintWriter writer = response.getWriter();
Enumeration<String> temp = c.getInitParameterNames();
while(temp.hasMoreElements()) {
writer.append(temp.nextElement());
}
writer.append("database: ").append(c.getInitParameter("database"))
.append(", server: ").append(c.getInitParameter("server"));
}
#Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
System.out.println("Servlet " + this.getServletName() + " has started.");
}
#Override
public void destroy() {
System.out.println("Servlet " + this.getServletName() + " has stopped.");
}
}
The way you have initialized things, I think what you are looking for should be available in
this.getServletConfig().getInitParameter("foo");
Broadly speaking the servlet context is shared by servlets inside a JVM but this object is present inside servlet config for a particular servlet, while <init-param> go in ServletConfig.
For more details I would strongly suggest to read the docs.

Issues configuring web.xml mapping

I have just picked up web development and I cannot for the life of me figure out what I'm doing wrong when mapping the SimpleServlet in web.xml
these are my files:
My web.xml is:
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>JavaHelloWorldApp</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>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>wasdev.sample.servlet.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/SimpleServlet</url-pattern>
</servlet-mapping>
</web-app>
Simple Servlet
package wasdev.sample.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SimpleServlet
*/
#WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().print("Hello World!");
}
}
I am simply trying to get a local host running and access the JavaHelloWorldApp/SimpleServlet

convert servlet-context.xml to ServletConfig.java

I am trying to convert my servlet-context.xml file to Servlet Config.java and there is no compile time error. But at run time, my server shows that no request mapping for /product/.
My web.xml is
<?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>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocatation</param-name>
<param-value>com.mds.test.ServetConfig</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>
and my ServletConfig is:
package com.mds.test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
#Configuration
#ComponentScan(basePackages={"com.mds.test"})
#EnableWebMvc
public class ServletConfig extends WebMvcConfigurerAdapter{
#Bean
public ViewResolver internalResourceViewer(){
InternalResourceViewResolver irvr= new InternalResourceViewResolver();
irvr.setPrefix("/WEB-INF/views/");
irvr.setSuffix(".jsp");
return irvr;
}
#Bean(name="multipartResolver")
public ExtendedMultipartResolver resolver(){
return new ExtendedMultipartResolver();
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// TODO Auto-generated method stub
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
Can you tell what I am missing. Thanks in advance
Sorry guys, I was doing a very little but confusing mistake. I was writing wrong class name. Class name should be com.mds.test.ServletConfig not the com.mds.test.ServetConfig in web.xml
So guys you can take it as a simple converter from xml to java file.

Servlet Mappings

My problem is i mapped my servlets on web.xml. When i submit info to servlet it gives me "resuorces is not found" error. How can i solve this problem. Thanks your answers.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<form action="Login" method="POST">
UserName:<input type="text" name="user_name">
<BR>
Password:<input type="password" name="password">
<BR>
<input type="submit" value="Gonder">
<BR>
<input type="reset" value="Reset">
</form>
</body>
</html>
SERVLET CODES are like this
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
private String usr_name;
private String pass;
static Logger log=Logger.getLogger(Login.class);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PropertyConfigurator.configure("C:\\Users\\AliArdaOrhan\\workspace2\\QuizProject\\WebContent\\WEB-INF\\log4j.properties");
usr_name=request.getParameter("user_name");
pass=request.getParameter("Password");
try {
if(Validation.validate(usr_name,pass)){
RequestDispatcher rs=request.getRequestDispatcher("Welcome");
rs.forward(request, response);
}
else{
RequestDispatcher rs=request.getRequestDispatcher("login");
rs.forward(request, response);
}
} catch (ClassNotFoundException | SQLException e) {
log.error("Cannot Validate User Information", e);
}
}
}
WEB XML CODES are like this
<?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" 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>QuizProject</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>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>Login Page</description>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Welcome</display-name>
<servlet-name>Welcome</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Welcome</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
</web-app>
You need to add a servlet mapping for Login action in your web.xml:
<servlet>
<description></description>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
And make sure you restart your server after you make those changes in web.xml.
And if you are using Servlet 3.0 then you can use annotations like below in your servlet which are equivalent to above code:
#WebServlet("/Login")
public class Login extends HttpServlet {
Please change on this line
<form action="Login" method="POST">
replace "Login" with "/GuestBook"
You are sending your form to Login (relative URL), but your servlet awaits at /GuestBook. Change the servlet mapping or form's action attribute.

Servlet not working on WebSphere 8.x

I have the following class...
package org.me.test
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/servlet")
public class TestServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/plain");
resp.getWriter().write("Test Servlet");
super.doGet(req, resp);
}
}
and the following 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">
<servlet>
<display-name>srv</display-name>
<servlet-name>srv</servlet-name>
<servlet-class>
org.me.test.TestServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>srv</servlet-name>
<url-pattern>/srv/*</url-pattern>
</servlet-mapping>
</web-app>
but I get...
Error 405: HTTP method GET is not supported by this URL
You are getting the 405 error because you are calling super.doGet() at the end of your doGet(). Try removing that line.
The default implementation of doGet() in the base class HttpServlet returns that 405 error. To support the GET operation, you must override doGet() without calling the parent implementation, otherwise it returns the same error intended to be displayed when there is no override.

Resources