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

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'.

Related

Why does my servlet request.getParameter return null?

I'm having a problem with the request. It always returns "null", but I don't know why. I want it to return a name.
This is my servlet:
public class MinServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Syvtabellen - fra en servlet</title></head>");
out.println("<body>");
out.println("<p>Her er syv-tabellen:<br>");
for (int i=1; i<=10; i++)
{
out.println("Syv gange "+ i +" er: "+ 7*i +".<br>");
}
out.println("</body>");
out.println("</html>");
String parameterværdi = request.getParameter("navn");
out.print( "Værdien af parameteren 'navn' er: <br>" + parameterværdi );
}
}
This is the index.xml:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
</body>
This is the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<servlet>
<servlet-name>MinServlet</servlet-name>
<servlet-class>konti.MinServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MinServlet</servlet-name>
<url-pattern>/MinServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
Do I've to add it in the index.xml somehow? I know that the parameter returns null if the parameter doesn't exist, but I don't know how to fix it :)
I believe you are following a tutorial. As Elliott said in his comment you need to have a parameter called "navn" in you view to catch it from your servelet otherwise you will get a null. Or else there should be a query string called "navn". Here I can't see any parameter called "navn" in your client side.
For example: http://www.java4s.com/java-servlet-tutorials/example-of-request-getparameter-retrieve-parameters-from-html-form/
According to your code:
index.html
<font face="verdana" size="2px">
<form action="getVal" method="post">
First way to pass request Param <input type="text" name="navn"><br>
<input type="submit" value="Submit">
</form>
</font>
TestApp.java
public class TestApp extends HttpServlet
{
protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
String n1=req.getParameter("navn");
pw.println("Requested Value" +n1);
pw.close();
}
}
web.xml
<web-app>
<servlet>
<servlet-name>sumOfTwoNumbers</servlet-name>
<servlet-class>java4s.OngetParameter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestApp</servlet-name>
<url-pattern>/getVal</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Or else you can pass query string as following
Second way to pass Request param:
index.html
Click here

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 not working with Tomcat 6.0.37

I have a development folder having following structure:
G:\MyProjects\beerV1--
|___classes\com\examples\web\BeerSelect.class
|_______src\com\examples\web\BeerSelect.java
|_____etc\web.xml
|_____web\form.html
My deployment folder hierarchy is:
C:\Tomcat6\apache-tomcat-6.0.37\webapps\Beer-v1
---WEB-INF\classes\com\examples\web\BeerSelect.class
---form.html
---WEB-INF\web.xml
When I run the http://localhost:8080/ , I can see the default tomcat page.
However I'm not able to see the form page using http://localhost:8080/Beer-v1/form.html( getting 404 error)
Am I missing something here?
The BeerSelect.java is:
package com.examples.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BeerSelect extends HttpServlet {
public void doHost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Beer Selection Advice<br>");
String c=request.getParameter("color");
out.println("<br>Got beer color "+c);
}
}
My web.xml is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
Version="2.4">
<servlet>
<servlet-name>Ch3Beer</servlet-name>
<servlet-class>com.examples.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Ch3Beer</servlet-name>
<servlet-class>/SelectBeer.do</servlet-class>
</servlet-mapping>
</web-app>
and my form.html is:
<html>
<!--Some sample HTML-->
<head>
<title>The english pub</title>
</head>
<body>
<h1 align="center">Beer Selection Page</h1>
<form method="POST"action="SelectBeer.do">
Select beer characteristics<p>
Color:
<select name="color" size="1">
<option value="light"> light </option>
<option value="amber"> amber </option>
<option value="brown"> brown </option>
</select>
<br><br>
<center><input type="SUBMIT"</center>
</form>
</body>
</html>
Do we have a method named doHost() in HttpServlet? I think your doHost() should be changed to doPost() as your action in html says to POST the request.
HTML:
<form method="POST"action="SelectBeer.do">
Servlet:
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Beer Selection Advice<br>");
String c=request.getParameter("color");
out.println("<br>Got beer color "+c);
}
}
Found something more:
in web.xml:
<servlet-class>/SelectBeer.do</servlet-class>
should be changed to :
<url-pattern>/SelectBeer.do</url-pattern>
And this resolved the problem.
Silly mistake.
Anyways thanks for looking into the issue for me.
Cheers!

404 with simple java servlet/html form [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.
ED: this question is not a duplicate. the alleged duplicate makes no mention of the error that was in my console, and the solution to my problem is not found in the linked question.
i'm using derek banas's tutorials to learn servlets right now. or at least, i'm trying to! i keep encountering a 404 error when i submit my form.
here is my code. first, the java class:
package helloservlets;
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 Lesson41
*/
#WebServlet
public class Lesson41 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 {
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 + "</body></html>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
my 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"
id="WebApp_ID" version="3.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>
and my html:
<!DOCTYPE html>
<html>
<head>
<meta charset="US-ASCII">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="http://localhost:8080/Lesson41/">
What's your name?<br /> <input name="yourname" /><br /> First
Number <input name="firstnum" /><br /> Second Number <input
name="secondnum" /><br /> <input type="hidden" name="Language"
value="English" /><br /> <input type="submit" />
</form>
</body>
</html>
in my console, i am getting this warning:
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Lesson41' did not find a matching property.
and that's the only warning i'm getting. i googled it and followed the advice i found here, but it hasn't fixed my problem.
thank you in advance for any help!
Are you sure the url in the form is correct? http://localhost:8080/Lesson41/
If you are using Eclipse, by default it deploys your application as http://localhost:8080/[Your-Project-Name-Here]/Lesson41/

JSP getParameters

I have a simple JSP project, which has 3 classes, controllers and DAO's
I can Login but when I click on my buttons, I just can't get the Parameters from the form.
Lemme show the code, and maybe You'll figure it out:
package school.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import school.dao.StudentDAO;
import school.model.Student;
public class StudentController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String UPDATE = "/student.jsp";
private static String VIEW_COURSES = "CourseController";
private static String VIEW_GRADES = "GradeController";
private StudentDAO dao;
public StudentController() {
dao = new StudentDAO();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String forward = "";
String action = request.getParameter("name");
if (action.equalsIgnoreCase("update")) {
forward = UPDATE;
Student student = (Student) request.getAttribute("student");
request.setAttribute("student", student);
} else if (action.equalsIgnoreCase("viewgrades")) {
forward = VIEW_GRADES;
Student student = (Student) request.getAttribute("student");
request.setAttribute("student", student);
} else if (action.equalsIgnoreCase("viewcourses")) {
forward = VIEW_COURSES;
Student student = (Student) request.getAttribute("student");
request.setAttribute("student", student);
}
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
And here's the 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">
<display-name>School_JSP</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>CourseController</display-name>
<servlet-name>CourseController</servlet-name>
<servlet-class>school.controller.CourseController</servlet-class>
</servlet>
<servlet>
<description></description>
<display-name>StudentController</display-name>
<servlet-name>StudentController</servlet-name>
<servlet-class>school.controller.StudentController</servlet-class>
</servlet>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>school.controller.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentController</servlet-name>
<url-pattern>/StudentController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CourseController</servlet-name>
<url-pattern>/CourseController</url-pattern>
</servlet-mapping>
</web-app>
And the mainPage.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>Main Page</title>
</head>
<body>
<h3>
Welcome,
<c:out value="${student.firstName}" />
</br>
<h1>My Personal Information</h1>
<center>
<form method="POST" action='StudentController' name="viewgrades">
<input type="submit" value="View My Grades" />
</form>
<form method="POST" action='StudentController' name="viewcourses">
<input type="submit" id="hidden" value="View My Courses" />
</form>
<form method="POST" action='StudentController' name="update">
<input type="submit" id="hidden" value="Update Personal Information" />
</form>
</center>
</h3>
</body>
</html>
You're confusing attributes and parameters.
Parameters are String values sent by the browser. They're accessed using the getParameterXxx() family of methods.
Attributes are data that you can add to a request (or session, or servlet context), or any type, in order to get them backlater. You typically add an attribute to the request in a servlet (for example, the information about the logged in user) in order to get back this attribute in the JSP and display the information.
In your code, the only place where you get a parameter is commented out. And you're trying to get a parameter named "hidden", although none of the form has an input field with than name. It's the name attribute of an input field that is submitted by the browser, and not its id attribute.
Also, you should use GET rather than POST for actions which consist in getting or reading things.
The reason you don't get any values is that the attribute "name" in the form, indeed is just the name of the form and isn't sent at all.
To actually send data, you need to provide some in the innerHTML of the form. Like this (not the best solution, but should you get running):
<form method="GET" action="StudentController" name="formName">
<input type="hidden" name="viewgrades" value="show" />
<input type="submit" value="View My Grades" />
</form>
After this you can read the sent data "viewgrades=show" (parameter=value) in your Java-class.
Also, btw, you shouldn't use "h3" as a paragraph ("p") or section. It is intended to be a headline.

Categories

Resources