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
Related
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 am new to servlets and jsp. I did a small application on Eclipse JEE version. But when I am executing I am getting 'http 404: resource not found' error even though the Tomcat has been installed properly on Eclipse. Here is the code I am using:
This is my ServletDemo.java
package com.advancejava;
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 ServletDemo
*/
#WebServlet(description = "ServletDemo", urlPatterns = { "/ServletDemo" })
public class ServletDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public ServletDemo() {
super();
// TODO Auto-generated constructor stub
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
try(PrintWriter pw = response.getWriter()) {
pw.println("<!DOCTYPE html>");
pw.println("<html>");
pw.println("<head>");
pw.println("<body>");
pw.println("<h1>Servlet Demo"+request.getContextPath()+"</h1>");
pw.println("</body>");
pw.println("</html>");
}
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
try{
pw.println("<html>");
pw.println("<head>");
pw.println("<title>Servlet</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<p>First DemoServlet</p>");
pw.println("</body>");
pw.println("</html>");
}
finally{
System.out.println("This is my Servlet Page");
}
}
}
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<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"
metadata-complete="true" version="3.0">
<display-name>ServletDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletDemo</servlet-name>
<servlet-class>com.advancejava.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo</servlet-name>
<url-pattern>/ServletDemo</url-pattern>
</servlet-mapping></web-app>
my index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Advance java
</body>
</html>
If there are any mistakes, please forgive me and suggest me how to correct them.
Since you've already declare your servlet at
#WebServlet(description = "ServletDemo", urlPatterns = { "/ServletDemo" })
don't declare twice in your web.xml.
the url to your servlet should be
http://localhost:8080/{contextPath}/ServletDemo
the contextPath will be your projectName by default if you use eclipse
there is one more thing, delete metadata-complete="true" in your web.xml. Otherwise it won't work.
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>ServletDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Previous response: The index page should be "my index.html" or "index.html".
It needs to go right with what is in the servlet.
index.html
Posting it twice really is the problem. I'm glad this has been resolved. it's helpful to me as well. Thanks
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.
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
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!
I have a Servlet which have form tag. In this form tag I want to call another servlet.
out.println("<form id=\"myform\" action='/SubmitHome' method=\"post\">");
So when I click submit button:
out.println("<input type=\"submit\" name=\"assignButton\" value=\"Assign\" />
It is not calling the "/SubmitHome".
Any idea why is this not calling?
Please check your HTML, Is your tag is proper closing?
e.g
<form action="" method="post">
<input type="submit" value="Submit">
</form>
Sytanx is wrong for below line (You need to complete the println braces:
out.println("<input type=\"submit\" name=\"assignButton\" value=\"Assign\" />
should be
out.println("<input type=\"submit\" name=\"assignButton\" value=\"Assign\" />");
Everything else is perfect and working fine :)
Here is the sample code to achieve your task.
Step1: Create a servlet.
package org.smarttechie;
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 SampleServlet1
*/
public class SampleServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public SampleServlet1() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<form action=\"SampleServlet2\" method=\"get\">");
out.println("<input type=\"submit\" value=\"Submit\">");
out.println("</form>");
out.println("</body></html>");
}
}
Step2: Created another servlet. This will be called from the form action tag. If you observe the form action tag value, that is pointing to the 'SampleServlet2' url pattern excluding of '/'. If you want to give absolute path(Inclusive of '/'), we need to specify with context path.
package org.smarttechie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SampleServlet2
*/
public class SampleServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public SampleServlet2() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("The request is reached");
}
}
Step3: The web.xml configuration is given 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">
<servlet>
<description></description>
<display-name>SampleServlet1</display-name>
<servlet-name>SampleServlet1</servlet-name>
<servlet-class>org.smarttechie.SampleServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet1</servlet-name>
<url-pattern>/SampleServlet1</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>SampleServlet2</display-name>
<servlet-name>SampleServlet2</servlet-name>
<servlet-class>org.smarttechie.SampleServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet2</servlet-name>
<url-pattern>/SampleServlet2</url-pattern>
</servlet-mapping>
</web-app>
Hope this solves your problem.
You should check url mapping in web.xml, that is the only reason form is not submitting on the desired page.