Can send data but request.getParameter always == null - java

There are my code from java eclipse keplero, my project was a servlet(server) that communicate with extension of chrome(client) via XMLRequest. I have used GET and POST and servlet can comunicate the string in between case but request.getParameter was in every case null.
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>XMLFinal</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>
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method = "get" action = "main">
Data: <input type="text" name="data">
<input type="submit" value = "send">
</form>
</body>
</html>
main.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import netscape.javascript.JSObject;
/**
* Servlet implementation class main
*/
#WebServlet("/main")
public class main extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public main() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
//server funziona in invio, il segnale in ricezione non funziona ancora
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//guardare file index.html
System.out.println(request.getParameter("data"));
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
//
response.getWriter().write("GET");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//guardare file index.html
System.out.println(request.getParameter("data"));
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
//invio segnale funzionante
response.getWriter().write("POST2");
}
private void getHeadersInfo() {
// TODO Auto-generated method stub
}
}
I have improve in login page to create a simple form and that run perfectly but i need data from js client :
loadXMLRequest();
function loadXMLRequest()
{
var xmlhttp;
//da testare su Chrome Firefox Opera IE 7 o superiore
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
//arrivo del segnale
xmlhttp.open("GET","http://localhost:8080/XMLFinal/main?ciao",true);
xmlhttp.setRequestHeader("Content-Type","text/plain");
alert("questi sono i dati da inviare : ciao"");
xmlhttp.send();
alert("Message sent");
//apertura pagina web risposta giunta ed in elaborazione
//window.open(xmlhttp.responseText);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==0)
{
alert("request not initialized");
}
if (xmlhttp.readyState==1)
{
alert("server connection established");
}
if (xmlhttp.readyState==2)
{
alert("request received");
}
if (xmlhttp.readyState==3)
{
alert("processing request");
}
if (xmlhttp.readyState==4)
{
alert("request finished and response is ready");
alert(xmlhttp.responseText);
//nessun dato ricevuto possibile shutdown del server
if(xmlhttp.responseText=="")
{
}
else
{
//windows.open(xmlhttp.responseText);
}
}
if (xmlhttp.readyState==200)
{
alert("OK");
alert(xmlhttp.responseText);
}
if (xmlhttp.readyState==404)
{
alert("Page not found");
}
}
}
else
{
//per IE 6 o inferiore
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
alert("Microsoft alert");
}
}

The Name is the Key
Your code says
System.out.println(request.getParameter("http"));
and your form says
<input type="text" name="data">
Change the first to
System.out.println(request.getParameter("data"));
or the second to
<input type="text" name="http">

You are making a Get call i.e.,
xmlhttp.open("GET","http://localhost:8080/XMLFinal/main?ciao",true);
So from servlet it will call doGet(-,-) method, but under doGet(-,-) method you have written
System.out.println(request.getParameter("http"));
which should be replaced like
System.out.println(request.getParameter("data"));
Which you have correctly written in doPost(-,-) method.
But i always prefer not to write redundant code. in both doGet() & doPost(), instead you can write code in a common method and call the same method from both the do*** methods. i.e.,
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Hope this will helps you.

Related

404 Requested Resource is not Available Eclipse/Tomcat [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I think I've read every question on this topic so far and I really tried a lot of solutions, so sorry if I overlooked anything.
I'm using Eclipse with Tomcat 8.
Tomcat is already configured as server and the MySQL-connector....jar is in the WEB-INF/lib folder, web.xml just in the /WEB-INF one and the index just in /WebContent
index.html:
<!DOCTYPE html>
<html>
<form action="Servlets/Start" method="post">
<font face="verdana" size="2">
Enter Table Name :<input type="text" name="table">
<input type="submit" value="Display">
</font>
</form>
</html
start.java:
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;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* Servlet implementation class Start
*/
#WebServlet("/Start")
public class Start extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Start() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse respond) throws ServletException, IOException {
PrintWriter pw=respond.getWriter();
respond.SetContentType("text/html");
String tb=request.getParameter("table");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc::oracle::thin:#localhost:music","root","1234");
Statement st=con.createStatement();
System.out.println("connection established successfully!");
ResultSet rs=st.executeQuery("SELECT * FROM"+tb);
pw.println("<table border=1>");
while(rs.next())
{
pw.println("<tr><td>"+rs.getInt(1)+"</td></td>+rs.getString(2)+</td>"+"<td>"+rs.getString(3)+"</td></tr>");
}
pw.println("</table>");
pw.close();
}
catch (Exception e){
e.printStackTrace();
}
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #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" version="3.1">
<display-name>Servlets</display-name>
<servlet>
<servlet-name>Start</servlet-name>
<servlet-class>start.Start</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Start</servlet-name>
<url-pattern>/Start</url-pattern>
</servlet-mapping>
<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>
Now whenever I run the index.html on the server, everything works fine, however after pressing the button it specifies a 404 Error and I have no idea why exactly this occurs.
I've tried using just /Start as action in index.html, which leads to the error message just pointing to /Start, however when I use Servlets/Start it is pointing to Servlets/Servlets/Start if that helps.
Entering localhost:8080/Servlets prompts me to index.html which then leads to the same problem
As I'm slowly getting very frustrated, I'd like to ask you for help, thanks in advance!
I have copied your source codes and created a web project in eclipse.
The Start.java (inside a package named start) is as follows:-
package start;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
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 Start
*/
#WebServlet("/Start")
public class Start extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Start() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse respond) throws ServletException, IOException {
PrintWriter pw = respond.getWriter();
respond.setContentType("text/html");
String tb = request.getParameter("table");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc::oracle::thin:#localhost:music", "root", "1234");
Statement st = con.createStatement();
System.out.println("connection established successfully!");
ResultSet rs = st.executeQuery("SELECT * FROM" + tb);
pw.println("<table border=1>");
while (rs.next()) {
pw.println("<tr><td>" + rs.getInt(1) + "</td></td>+rs.getString(2)+</td>" + "<td>" + rs.getString(3)
+ "</td></tr>");
}
pw.println("</table>");
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
// TODO Auto-generated method stub
// response.getWriter().append("Served at:
// ").append(request.getContextPath());
}
/**
* #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);
}
}
The web.xml has been modified to remove servlet mapping as it exists as #WebServlet on the class itself. The content is as follows:
<?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>Servlets</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>
The index.html (in WebContent folder) is as follows:
<!DOCTYPE html>
<html>
<form action="Start" method="post">
<font face="verdana" size="2">
Enter Table Name :<input type="text" name="table">
<input type="submit" value="Display">
</font>
</form>
</html>
The result is that I can access the web application at http://localhost:8080/Servlets/ and by clicking on the display button, go to the next page as well.
In index.html section change the action to action="Start".
In web.xml put the full qualified class name in

Ajax call from JavaScript to servlet

I am trying to make AJAX call from html file to a servlet class to pass a variable but not getting any output. Please check the code below and suggest the changes that should be done.
This is my project structure
FrontPage.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function func() {
var build = document.getElementById('name').textContent;
$.ajax({
type : 'GET',
url : 'http://localhost:8081/Sample/TestServlet',
data : build,
success : function(data) {
alert("Success");
},
error : function() {
alert("Error");
}
});
}
</script>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a id="name" href="#" onClick="func();">Build1</a>
</body>
</html>
This is TestServlet.java. This is trying to print variable which is passed from javascript function using Ajax call:
package com.infy;
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;
/**
* Servlet implementation class TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println(request.getParameter("build"));
}
}
Web.xml file
<?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>Sample</display-name>
<welcome-file-list>
<welcome-file>FrontPage.html</welcome-file>
</welcome-file-list>
<servlet>
<description>Sample Servlet</description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.infy.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
I want to send the build variable to the servlet class and print the same from the servlet class but not getting any output.Please help me in fixing this.
you are sending the request as GET method and you are not passing the variable in url.Try to print the build value in servlet first and check weather it is coming to servlet or not.
You should send the GET request as :-
url : "/TestServlet?build="+build+",
type : "GET"
,
And watch out the URL also.
in servlet site create a json object.Like
JSONObject jsonObj = new JSONObject();
jsonObj.put("build", build);
Try it and let me know.

Form submit not working in servlet application

Below are the web.xml, servlet and jsp code
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;
/**
* Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String userName = request.getParameter("userName");
PrintWriter out = response.getWriter();
out.println("hello "+userName+" how are you ?");
}
}
JSPCode:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is First Servlet
<form action="firstTest">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td> Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input name = "sumbit "type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Web.xml Code:
<?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>Trial</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/firstTest</url-pattern>
</servlet-mapping>
</web-app>
when i give URL http:localhost:8080/Trial the Index.jsp is coming but when i gave username and password the URL is chaning to http:localhost:8080/firstTest instead of http:localhost:8080/Trial/firstTest and i am getting 405 error "Tomcat error HTTP Status 405 - HTTP method GET is not supported by this URL" is anything wrong in my code
typo...
protected void doGost
change to doPost or doGet...
In your jsp code form tag you have not specified method name, and by default it use GET method, so just replace this code, I hope it will work :
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 MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
PrintWriter out = response.getWriter();
out.println("hello " + userName + " how are you ?");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
For to resolve this type of typo errors before we deploy into the server
We use #override annotation for to test whether the method is overloaded method or not. When ever you compiled the java it will throw exception. Actually compiler will check for syntactic errors etc. But in this scenario compiler treated doGost() is also a method by using above annotation at the compilation level only developer can understand where the problem occurred.
So. My suggition is #override for overriding methods.

Servlet not displaying anything while called

I am just writing the code after a while for servlet/jsp.
When I am calling servlet it is giving me blank browser and even not an error.
My Jsp :
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="HelloServlet" method="POST">
Please enter a color <br>
<input type="text" name="color"size="20px">
<input type="submit" value="submit">
</form>
</body>
</html>
My Servlet :
import java.io.IOException;
import java.io.PrintWriter;
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;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#service(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Servlet called...");
String color = request.getParameter("color");
PrintWriter out = response.getWriter();
/*
* out.println(
*
* "<html> \n" + "<head> \n" +
* "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n"
* + "<title> My first jsp </title> \n" + "</head> \n" + "<body> \n" +
* "<font size=\"12px\" color=\"" + color + "\">" + "Hello World" +
* "</font> \n" + "</body> \n" + "</html>");
*/
out.println("Aman.............");
}
/**
* #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);
}
}
And 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_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<display-name>TestApp</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>
Please help me out in this.
Thanks,
Aman
Add super to the following function and everything will work
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
**super(request, response) // add this**
}
The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers).
You have overrided the function which is responsible for calling doget and post.
Hope this solves your query
The mistake you made is you are using both annotation as well as registering the servlet through deployment descriptor (web.xml).
If you use annotation,registering the servlet through deployment descriptor is not required. But you should have tomcat7 as it will not run in the previous versions of tomcat. #WebServlet annotation is used to map the servlet with the specified name.
Moreover I assume that you are not using any package because in the url-mapping you dint specify one. But in case you are using some package named mypackage and your servlet class is inside it then change the url-mapping to something like this
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>mypackage.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
The protected service method checks the type of request, if request type is get, it calls doGet method, if request type is post, it calls doPost method, so on
try to remove this method,and your servlet should work
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
or rewrite your service method using this :
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String method = request.getMethod();
if(method.equals("GET"))
doGet(request, response);
else
doPost(request, response);
}
You don't write anything in service method so that it will never call either doPost or doGet

How to run servlet in Eclipse?

Installation Details:
First I install the jdk then add a environment variable
variable name = "path"
variable value ="C:\Program Files\Java\jdk1.7.0_45\bin"(directory of java)
Then Extract the eclipse and tomcat
After extracting, I edit the catalina.bat from apache-tomcat-7.0.23\bin and add this:
"set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45"
Then I made my first Servlet by creating new dynamic web project
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Form</title>
</head>
<body>
<h1>JAVA TEST</h1>
<form action ="work.html" method ="get">
<input type="submit" value="login">
</form>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
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 work
*/
#WebServlet("/work.html")
public class work extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public work() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<head>");
out.print("<title>Login Authentication Result</title>");
out.print("<h2>Welcome to servlet</h2>");
out.print("<p> powered by" + getServletContext().getServerInfo()+ "</p>");
out.print("</body>");
out.print("</hmtl>");
out.close();
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
<?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>Test3</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>
I'm new to servlet. I don't know why it is always have this error when clicking the button in the index.html
I think I have the same problem
Just configure Tomcat in eclipse!

Categories