HTTP status 500 : Error instantiating servlet class [com.test.Login] - eclipse

I was following a tutorial, while tring to run this little code I got this error :
HTTP status 500 : Error instantiating servlet class [com.test.Login]
Would you like please to help me to figure a solution
The Servlet :
package com.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
public Login() {
// TODO Auto-generated constructor stub
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Welcome !");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
The web.xml :
//In the beggining I had an error in those two first lines just by creating a Dynamic web project before even writing anything so I copied them from a solution of the same problem on stackoverflow, I hope it's not the reason of the error now.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4">
<display-name>Test</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></description>
<display-name>Login</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>com.test.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
Error I got :
javax.servlet.ServletException: Error instantiating servlet class [com.test.Login]
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:872)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1695)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:831)

Related

Servlet 404 error on get request upon submitting the form

Getting a 404 error on click on submit button on the form. Getting error (The requested resource is not available.) for the URL mapping http://localhost:8080/HelloWorld/HelloServlet. I tried the reference this as well, Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available" but this also doesn't seem to work.
1, index,html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="HelloServlet">
<input type="submit" value="HIT">
</form>
</body>
</html>
2. HelloServlet
package com.example.aman;
import java.io.IOException;
import javax.servlet.ServletConfig;
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 HelloServlet
*/
#WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public HelloServlet() {
// TODO Auto-generated constructor stub
}
/**
* #see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("DIVESH");
}
/**
* #see HttpServlet#doGet(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());
response.getWriter().println("DIVESH");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3. web.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>HelloWorld</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>
</web-app>
http://localhost:8080/HelloWorld/HelloServlet.
Straight away that tells me what the problem is. You are trying to access a servlet from within the same folder directory. This is what happens if you are not careful with your relative URLS
In your form, change the URL mapping to this:
<form action="/HelloServlet">
<input type="submit" value="HIT">
</form>
Or if that didn't work, do this:
<form action="../HelloServlet">
<input type="submit" value="HIT">
</form>
Adding a / to the path makes it absolute.
Adding ../ makes it go to the parent of the current directory.
Hope that helps.

Servlet, GET and POST form problems

I have a problem with only one servlet, all the others (declared in the same way) work nice
Servlet with problem :
package com.myproject.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#SuppressWarnings("serial")
public class Connexion extends HttpServlet {
public static final String VUE = "/WEB-INF/connexion.jsp";
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
}
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
}
}
Another Servlet who works :
package com.myproject.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#SuppressWarnings("serial")
public class Authentification extends HttpServlet {
public static final String VUE = "/WEB-INF/authentification.jsp";
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
}
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException{
}
}
The JSP who call the servlet "Connexion" :
<form id="form_authentification" class="formulaire" method="post" action="connexion">
<input type="text" class="input_text" title="<fmt:message key="mail_ou_nom" />" placeholder="<fmt:message key="mail_ou_nom" />" required />
<input type="submit" class="btn" value="<fmt:message key="suivant" /> "/>
And the web.xml :
<!-- Authentification -->
<servlet>
<servlet-name>Authentification</servlet-name>
<servlet-class>com.myproject.servlets.Authentification</servlet-class>
<init-param>
<param-name>auteur</param-name>
<param-value>Atyss</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Authentification</servlet-name>
<url-pattern>/authentification</url-pattern>
</servlet-mapping>
<!-- Connexion -->
<servlet>
<servlet-name>Connexion</servlet-name>
<servlet-class>com.myproject.servlets.Connexion</servlet-class>
<init-param>
<param-name>auteur</param-name>
<param-value>Atyss</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Connexion</servlet-name>
<url-pattern>/connexion</url-pattern>
</servlet-mapping>
So If I put the method "get" in the form : "form_authentification" of the JSP authentification.jsp it works but I have in line the browser :
http://localhost:8080/myproject/connexion?
Where from "?" is coming in the line browser?
If I put the method "post" in the form : "form_authentification" of the JSP authentification.jsp I have a white page for connexion.jsp and the browser tag icon of apache server.
Thank you for your answer

HTTP method GET is not supported by this URL with simple servlet

i'm trying to solve this problem since 2 days, there are the same title of problems in many post on stackoverflow but it was all different, because they allways had an error in code, but in my case i have a sample servlet, and when i run the project i get this error
HTTP Status 405 - HTTP method GET is not supported by this URL
this is my class servlet
package web;
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;
public class Controller extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public void init() throws ServletException {
}
#Override
protected void doGet(HttpServletRequest requet, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out =response.getWriter();
out.println("hello");
}
}
and 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>test2</display-name>
<servlet>
<servlet-name>cs</servlet-name>
<servlet-class>web.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cs</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
please tell me how solve this problem, thanks ...
Simple doGet Servlet Or You Can add try catch blog to debug code
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class logout extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=UTF-8");
out.println("Hello");
}
catch(Exception e)
{
out.println(e.toString())
}
}
}
I think you probably have not add GAT method on form attribute Like this
<form action="web" method="GET"></form>

Requested resource isn't available (Servlet)

I created a dynamic web project in Eclipse 4.5, called HelloServlet and modified web.xml. I also added Tomcat 8 in Eclipse. I think I did everything right, but I still can't run the servlet.
In Project explorer: /src/HelloServlet/HelloWorldServlet.java.
HelloWorldServlet.java:
package HelloServlet;
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 HelloWorldServlet
*/
#WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().println("Hello world.");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
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>HelloServlet</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>HelloWorldServlet</servlet-name>
<servlet-class>HelloServlet.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>
</web-app>
Sometimes, I get class not found without /HelloWorldServlet. I searched and knew how to set servlet-class correctly, but it still doesn't run.
It seems in newer versions (I don't know of what. Servlet or Eclipse?), I don't have to add anything to web.xml. #WebServlet("/HelloWorldServlet") seems to be enough. I discovered it by trying.
But I noticed strange behavior: While web.xml was modified as in the question, I could run the server then get errors in the browser like Reuqested resource isn't available or class not found exception in /HelloServlet & /HelloServlet/HelloWorldServlet. After restarting the computer, I couldn't run the server (I think, because of modifying web.xml). I don't remember adding anything new.

RESTful Service using Jersey

Please find the following code.
Service:DataResource.java
package com.mypack.pack2;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import com.mypack.pack1.DataClass;
#Path("data")
public class DataResource {
//Just retrieves the data members of the class
//i.e., 10 Ram
// Able to retrieve successfully.
#GET
#Produces("text/plain")
public String display()
{
DataClass obj1=new DataClass();
return obj1.getId()+obj1.getName();
}
#POST
#Path("/{id}/{name}")
#Produces("text/plain")
#Consumes("text/plain")
public void newData(#PathParam("id") int no,
#PathParam("name") String name) {
DataClass obj= new DataClass();
obj.setData(name,no);
System.out.println("Success");
System.out.println("Data after changes"+obj.getId()+obj.getName());
}
//TodoDao.instance.getModel().put(id, todo);
}
DataClass.java
package com.mypack.pack1;
public class DataClass {
private String ename="Ram";
private int eno=10;
public void setData(String name,int no)
{
this.ename=name;
this.eno=no;
}
public int getId()
{
return eno;
}
public String getName()
{
return ename;
}
}
Web.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" 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>de.vogella.jersey.jaxb</display-name>
<servlet>
<servlet-name>Jersey REST Service</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>com.mypack.pack2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
I am not able to change the values of class members ename and eno of DataClass. Can anyone please tell me why it is not changing? Is it because i am trying the code in a wrong way?
How are you invoking the POST URI (localhost:8080/JerseyProject/rest/data/11/John)? Be sure you are not invoking it from your browser, cause this way you would be invoking the verb GET o the /data/{id}/{name} that doesn't have implementation. That would explain why you're getting the status 405.
Usually the CREATE operation is used using the HTTP VERB POST on the collection URI with its params in the payload not on the path. In this case using POST on /data instead of /data/{id}/{name}.