Its my first servlet program and I have issues [duplicate] - java

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 3 years ago.
My index looks like this:
<HTML>
<head>
<title> JSP Servlet Example</title>
</head>
<body>
<div align="center" style="margin-top: 50px;">
<form action="ServletManager">
Please enter your Username: <input type="text" name="username" size="20px"> <br>
Please enter your Password: <input type="text" name="password" size="20px"> <br><br>
<input type="submit" value="submit">
</form>
</div>
</body>
</HTML>
My servlet:
package helloweb;
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 ServletManager extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// reading the user input
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
out.println (
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" +" +
"http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html> \n" +
"<head> \n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; " +
"charset=ISO-8859-1\"> \n" +
"<title> JSP Servlet Example </title> \n" +
"</head> \n" +
"<body> <div align='center'> \n" +
"<style= \"font-size=\"12px\" color='black'\"" + "\">" +
"Username: " + username + " <br> " +
"Password: " + password +
"</font></body> \n" +
"</html>"
);
}
}
My XML:
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
JSPServletExample
<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>Hello</servlet-name>
<servlet-class>helloweb.ServletManager</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/ServletManager.java</url-pattern>
</servlet-mapping>
Here is my issue:
Firefox can’t find the file at /C:/Users/HP/IdeaProjects/ourdemo /web/ServletManager?username=hello&password=hello.`
I host at the tomcat 7 server.
The first page appears. I type in the username and password, and I get this.
Please help.

Please change the url pattern change this in web.xml from /ServletManager.java to /ServletManager and try
https://javabelazy.blogspot.com/2010/02/student-registration-form-in-java.html (Working code)

Related

404 error with java servlets. can't see html file

I am following derek banas' tutorial on youtube (https://www.youtube.com/watch?v=_HnJ501VK3M) and when I try to run the code the browser displays a status-404 and
Message: /Lesson41/
Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
I was getting IllegalArguementsException saying something about 2 different servlets being mapped to the same url-pattern. So I removed the #WebServlet annotation.
Now I'm getting the 404 and I don't know what the cause is. I think eclipse doesn't see the sayhello.html
Here is the code:
Servlet: Lesson41.java
public class Lesson41 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String usersName= request.getParameter("yourname");
String theLang = request.getParameter("Language");
int firstNum = Integer.parseInt(request.getParameter("firstnum"));
int secondNum = Integer.parseInt(request.getParameter("secondnum"));
int sumONum = firstNum + secondNum;
response.setContentType("text/html");
PrintWriter output = response.getWriter();
output.println("<html><body><h3>Hello " + usersName);
output.println("</h3><br />" + firstNum + " + " + secondNum);
output.println(" = " + sumONum + "<br />Speaks " + theLang);
output.println("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
The 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_4_0.xsd" id="WebApp_ID" version="4.0">
<servlet>
<servlet-name>Lesson41</servlet-name>
<servlet-class>helloservlets.Lesson41</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Lesson41</servlet-name>
<url-pattern>/Lesson41</url-pattern>
</servlet-mapping>
</web-app>
html: sayhello.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<form method="post" action="http://localhost:8080/Lesson41/">
What's your name?<br />
<input name="yourname" /><br />
First Number<br />
<input name="firstnum" /><br />
Second Number<br />
<input name="secondnum" /><br />
<input type="hidden" name="Language" value="English" /><br />
<input type="submit" />
</form>
</body>
</html>
The directory structure:
directory Structure
Expected Output:
It should show a form with fields for name, number1, number2 and a submit button. It correctly goes to localhost:8080/Lesson41/ but can't see the html.
Update the url-pattern to /Lesson41/
<url-pattern>/Lesson41/</url-pattern>
form action change to /Lesson41/ and try. might help you.
<form method="post" action="/Lesson41/">
</form>
Also, while mapping you are using servlet-class as 'helloservlets.Lesson41', need to add the package. 'helloservlets' in servlet 'Lesson41'.

Error HTTP Status 404. The requested resource is not available. [Netbeans]

I do a project Fahrenheit to Celsius conversion using netbeans with Tomcat 8.0.37
when i try to run project, i get a issue HTTP Satus 404.
My index.html
<html>
<head>
</head>
<body>
<h3>Please enter Fahrenheit temperature:</h3><p>
<form action="/conv/test">
Temperature(F) : <input type="text" name="temperature"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
My web.xml
<web-app>
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>doGetMethod.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
My TestServlet.java
public class TestServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException
{
String temperature = req.getParameter("temperature");
DecimalFormat twoDigits = new DecimalFormat("0.00");
try
{
double tempF = Double.parseDouble(temperature);
String tempC = twoDigits.format((tempF -32)*5.0/9.0);
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("<h3>" + temperature + " Fahrenheit is
converted to " + tempC + " Celsius</h3><p>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"There was an input error");
}
}
}
Please help me to solve this issue !
I apologize for not being word-perfect in English.
You should access the index.html at localhost:8080/contextRoot/index.html. The action associated to the form should map to the servlet, so it should be action="/test". The servlet-class tag in the web.xml should specify your servlet class full name such as mypackage.TestServlet. You could avoid using a web.xml and save you some time by using annotation on the Servlet class as well explained here https://docs.oracle.com/javaee/7/tutorial/servlets004.htm#BNAFU. Also look here for a similar example https://stackoverflow.com/a/2395262/6848537

Tomcat and mixing POST and GET params inside a request

I have read question: HTTP POST with URL query parameters and understood that it possible to do it but with java and tomcat I cannot manage it.
I have html page:
<!DOCTYPE html>
<html>
<body>
<form action="HelloForm" method="POST">
First Name: <input type="text" name="first_name">
<br/>
Last Name: <input type="text" name="last_name"/>
<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
And I send http://localhost:8282/Hello.html?uri_param=pamparam clicking on submit button.
I tracked by proxy that both uri(GET like) and body(POST like) params have been sent:
Referer: http://localhost:8282/Hello.html?uri_param=pamparam
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 36
first_name=Sergei&last_name=Rudenkov
But I get null when execute request.getParameter("uri_param"); inside doPost method.
So the question is: Is it possible to mix POST and GET params using tomcat?
Edited(additional info was requested):
My web.xml:
<?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">
<servlet>
<servlet-name>HelloForm</servlet-name>
<servlet-class>HelloForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloForm</servlet-name>
<url-pattern>/HelloForm</url-pattern>
</servlet-mapping>
</web-app>
My servlet:
public class HelloForm extends HttpServlet {
String uri_param;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Using GET Method to Read Form Data";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"<li><b>URI PARAM</b>: "
+ uri_param + "\n" +
"</ul>\n" +
"</body></html>");
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
uri_param = request.getParameter("uri_param");
doGet(request, response);
}
}
Result:
The way I see it, you added that GET-Parameter to the call to Hello.html, not to the actual Form-Submit to /HelloForm
Try this:
<!DOCTYPE html>
<html>
<body>
<form action="HelloForm?uri_param=janWasRight" method="POST">
First Name: <input type="text" name="first_name">
<br/>
Last Name: <input type="text" name="last_name"/>
<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
And maybe do
// Method to handle POST method request.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
uri_param = request.getParameter("uri_param");
System.out.println("Parameter uri_param: " + uri_param);
doGet(request, response);
}
Edit
You can even see that in your screenshot: There's no ?uri_param in the URL you POSTed

Servlet: HTTP Status 404 - The requested resource is not available [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'm new to Java Programming & having a tough time with Servlets & JSP, for the wide range of challenges it throws. For now, I'm unable to access the Servlet page due to this error:
HTTP Status 404 - The requested resource is not available
This might seem to be a naive question for many, however after trying all the tips and tricks ranging from Stack Overflow to resorting to other study materials, I couldn't figure out the exact cause of the problem.
Servlet file:
package coreservlets;
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;
#WebServlet("/GoodCodeServlet")
public class GoodCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Code Sample";
String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType + "<html> \n" +
"<head> <title>" +title+ "</title></head>" +
"<body bgcolor=\"#eee\">" +
"<h1 align=\"center\">" +title+ "</h1>" +
// Text inside a <pre> tag is displayed in a fixed-width font,
//and it preserves both spaces and line breaks....
"<pre> \n" + getCode(request)+ "</pre>" +
"</body> </html>"
);
}
protected String getCode(HttpServletRequest request)
{
return (request.getParameter("code"));
}
HTML file:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#FDEFD6">
<center> <h1>Submit Code Samples</h1>
<form action="/coreservlets.GoodCodeServlet" >
Code: <br><br>
<textarea rows="12" cols="40" name="code"></textarea> <br><br>
<input type="submit" value="submit" />
</form>
</center>
</body>
</html>
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_3_0.xsd" version="3.0">
<servlet>
<servlet-name>CodeSample</servlet-name>
<servlet-class>coreservlets.GoodCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CodeSample</servlet-name>
<url-pattern>/coreservlets.GoodCodeServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ShowParameters</servlet-name>
<servlet-class>/coreservlets.ShowParameters</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowParameters</servlet-name>
<url-pattern>/coreservlets.ShowParameters</url-pattern>
</servlet-mapping>
</web-app>
Context path is missing from form action.
Please refer :
Use Relative Path for Form Actions in jsp
Form Action sampleservlet giving me exception

Calling servlet from HTML form, but servlet is never invoked [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.
Iam calling servlet from html form,servlet takes the form data and it will insert that form data into database.But when i click the submit button error page is coming.please help whats wrong in my servlet code.
my servlet code:
import javax.servlet.http.HttpServlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Loginservlet extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
System.out.println("login servlet");
String connectionURL = "jdbc:mysql://localhost:3306/mysql";
Connection connection=null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String username= req.getParameter("username");
String password = req.getParameter("password");
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "root");
String sql = "insert into signup values (?,?)";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, username);
pst.setString(2, password);
int numRowsChanged = pst.executeUpdate();
out.println(" Data has been submitted ");
pst.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: "+ e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
out.println(e);
}
finally {
try {
if (connection != null)
connection.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}
}
}
my html code:
Sign Up
<form action="servlet/Loginservlet" method="post" >
<font size='5'>Create your Account:</font><br/><br>
<label for="username" accesskey="u" style="padding-left:3px;">User Name: </label>
<input type="text" style="background-color:#ffffff;margin-left:14px;padding-top:7px;border-width:0px;margin-top:6px;padding-right:85px;" id="username" name="username" tabindex="1"><br/><br>
<label for="password" accesskey="p" style="padding-left:4px;">Password: </label>
<input type="password" style="background-color:#ffffff;margin-left:14px;padding-top:7px;border-width:0px;padding-right:85px;" id="password" name="pasword" tabindex="2"><br/><br>
<input type="submit" value="Submit" style="margin-left:164px;"/>
<input type="reset" value="Reset" style="margin-left:17px;"/>
</form>
web.xml file:
<?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">
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>Loginservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
please help
check web.xml file of your project, you have to registrate your servlet there.check this also
use <form action="/login" method="post" > in html
and
in web.xml
<servlet-class>your.class.package.Loginservlet</servlet-class>
</servlet>
As you look into your servlet class, there is no package defined, which is required. And map that class with package(mean fully qualified name) in <servlet-class/> tag.
Another thing is you are setting action to url servlet/LogininServlet, but given different url in <url-pattern/> tag, which is wrong. you can simply set the form action to login
everything is fine....in the html page use action="./login".........it will work i have done the same

Categories

Resources