Servlet not working with Tomcat 6.0.37 - java

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!

Related

method="post" in form tag of html does not work in servlet in java

method="post" does not work in servlet
any help would be appreciated. Thanks in advance.
I get error of -- HTTP method GET is not supported by this URL-- eventhough i use dopost method in my servlet class.
Here is my code:
index.html
<!DOCTYPE html>
<html>
<body>
<form action="add" method="post">
Enter 1st number: <input type="text" name="num1">
<br>
Enter 1st number: <input type="text" name="num2">
<br>
<input type="submit">
</form>
</body>
</html>
AddServlet.java
package com.practice;
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 AddServlet extends HttpServlet
{ private static final long serialVersionUID = 1L;
#Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
int i = Integer.parseInt(req.getParameter("num1"));
int j = Integer.parseInt(req.getParameter("num2"));
int k = i+j;
System.out.println("result is:" + k);
PrintWriter out1 = res.getWriter();
out1.println("result is: dopost method" + k);
}
}
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="WebAppID" version="4.0">
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.practice.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
</web-app>

The package javax.servlet.http is accessible from more than one module: servlet.api, tomcat.i18n.es, tomcat.i18n.fr, tomcat.i18n.ja [duplicate]

This question already has answers here:
How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
(16 answers)
Closed 9 days ago.
While I'm Running my index.html file I'm getting a Error
Which looks like this[enter image description here][1]
And after checking all the files(web.xml,index.html and the java servlet)file I found this
[enter image description here][2]
Which says
"The package javax.servlet.http is accessible from more than one module: servlet.api, tomcat.i18n.es, tomcat.i18n.fr,
tomcat.i18n.ja"
What to do?
package com.kmit.sonet;
import java.util.Arrays;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutpuxtStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//Extend HttpServlet class
public class Capital extends HttpServlet {
private static final long serialVersionUID = 1L;
public Capital() {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Select a state";
String responseString = "empty";
String Capof = request.getParameter("state");
out.println("<html>");
out.println("<head><title>" + title + "</title></head>");
out.println("<body>");
out.println(" <b>" + Capof + " is " + responseString + "</b>");
out.println("</body>");
out.println("<html>");
if(Capof=="Andhra Pradesh")
{
responseString="Hyderabad";
}
}
// function to give out the capital as output
}
<?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>PrimeChecker</display-name>
<!-- Define servlet, named primeNumber -->
<servlet>
<servlet-name>City</servlet-name>
<servlet-class>com.kmit.sonet.Capital</servlet-class>
</servlet>
<!-- Defines the path to a2ccess this Servlet -->
<servlet-mapping>
<servlet-name>City</servlet-name>
<url-pattern>/Capital</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>
</web-app>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<select id="State" name="Capof">
<option value="Andhra Pradesh" >Andhra Pradesh</option>
<option value="Arunachal Pradesh">Arunachal Pradesh</option>
<option value="Assam">Assam</option>
<option value="Bihar">Bihar</option>
<option value="Chandigarh">Chandigarh</option>
<option value="Chattisgarh">Chattisgarh</option>
<option value="Goa">Goa</option>
<option value="Gujarat">Gujarat</option>
<option value="Hryana">Haryana</option>
<option value="Himachal Pradesh">Himachal Pradesh</option>
<option value="Jammu and Kashmir">Jammu and Kashmir</option>
<option value="Jharkhand">Jharkhand</option>
<option value="Karnataka">Karnataka</option>
<option value="Kerala">Kerala</option>
<option value="Madhya Pradesh">Madhya Pradesh</option>
<option value="Maharashtra">Maharashtra</option>
<option value="Manipiur">Manipur</option>
<option value="Meghalaya">Meghalaya</option>
<option value="Mizoram">Mizoram</option>
<option value="Odisha">Odisha</option>
<option value="Punjab">Punjab</option>
<option value="Rajasthan">Rajasthan</option>
<option value="Sikkim">Sikkim</option>
<option value="Tamil Nadu">Tamil Nadu</option>
<option value="Telangana">Telangana</option>
<option value="Tripura">Tripura</option>
<option value="Uttar Pradesh">Uttar Pradesh</option>
<option value="Uttarakhand">Uttarakhand</option>
<option value="West Bengal">West Bengal</option>
</select>
<input type="submit" value="Submit" />
</body>
</html>
this error is coming due to servlet jar is added by multiple source. If you added servlet jar from web server kindly check build path either multiple version of web server will be at build path
Solution
Right----"Properties----> Java build Path----> Libraries----> ClassPath---->add Library----> Server R Untime----> Next----> select tomcat----> Finish

getPart() returns null on uploading a file using servlets i have included #MultipartConfig but still it returns null

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import com.oreilly.servlet.*;
import javax.servlet.http.Part;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.*;
#MultipartConfig
#WebServlet("upload")
public class FileUploadServlet extends HttpServlet
{
#Override
public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Part filePart =req.getPart("fileName");
String realPath = getServletContext().getRealPath("file");
MultipartRequest mpr = new MultipartRequest(req,realPath,500*1024*1024);
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
if (filePart != null) {
// prints out some information for debugging
out.println(filePart.getName());
out.println(filePart.getSize());
out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
out.println("<html><body>");
//out.println(fileName);
out.println("File uploaded successfully");
out.println("</body></html>");
}
}
and the html is
<html>
<head>
<title>form</title>
<body>
<h2>Upload Contribution</h2>
<br><br>
<p>Contribution Domain: <input type="text" name="contdomain">
<br>
<br>
Contribution Name: <input type="text" name="contname">
<br>
<br>
<form method="post" action="upload" name="submit" enctype="multipart/form-data">
<input type="file" name="fileName"><br /><br />
<input type="submit" name="submit" value="Submit">
</form>
</head>
</body>
</html>
and the web.xml is
<web-app>
<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<servlet-class>FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadServlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
</web-app>
kindly help me to resolve this problem... all where i have searched it asks to add #MultipartConfig but adding it still doesnt give the result.
i need the file name so that i cud add the file to database
The problem is with Servlet declaration, you can remove <servlet></servlet> tag from web.xml file (with everything what's inside), and add the following proper annotation at the top of your Servlet class, so here is full version, which works on my machine:
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.Part;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.*;
#MultipartConfig
#WebServlet(
name = "FileUploadServlet",
urlPatterns = { "/upload"},
loadOnStartup = 1
)
public class FileUploadServlet extends HttpServlet
{
#Override
public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Part filePart =req.getPart("fileName");
InputStream inputStream = filePart.getInputStream();
}
}
The HTML:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>App</title>
</head>
<body>
<form method="post" action="upload" name="submit" enctype="multipart/form-data">
<input type="file" name="fileName"><br /><br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
web.xml file:
<?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">
<display-name>App</display-name>
</web-app>

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.

Basic Servlet doesn't work

I tried to create a simple servlet from the book, but to no avail.
I use GlassFish Server Open Source Edition 3.1.2.2, jdk1.7.0_10, Notepad.
root\WEB-INF\classes\net\ensode\glassfishbook\formhandling\FormHandlerServlet.class :
package net.ensode.glassfishbook.formhandling;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FormHandlerServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,HttpServletResponse response)
{
String enteredValue;
enteredValue = request.getParameter("enteredValue");
response.setContentType("text/html");
PrintWriter printWriter;
try
{
printWriter = response.getWriter();
printWriter.println("<p>");
printWriter.print("You entered: ");
printWriter.print(enteredValue);
printWriter.print("</p>");
}
catch (IOException e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
printWriter.println("<h2>");
printWriter.println("If you are reading this, your application server is good to go!");
printWriter.println("</h2>");
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
}
}
root/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>dataentry.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FormHandlerServlet</servlet-name>
<servlet-class>
net.ensode.glassfishbook.formhandling.FormHandlerServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormHandlerServlet</servlet-name>
<url-pattern>/formhandlerservlet</url-pattern>
</servlet-mapping>
</web-app>
root/dataentry.html :
<!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>Data Entry Page</title>
</head>
<body>
<form method="post" action="formhandlerservlet">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>Please enter some text:</td>
<td><input type="text" name="enteredValue" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
I create the WAR file by the following command in the console:
cd e:\root\
jar cvf formhandling.war *
Next, I copy my WAR file into the ‘autodeploy’ directory.
The link: http://localhost:8080/formhandling/
gives me the error: HTTP Status 404, description - The requested resource () is not available.
The link: http://localhost:8080/formhandling/dataentry.html
gives me the error: HTTP Status 404, description - The requested resource () is not available.
The link: http://localhost:8080/formhandling/formhandlerservlet
gives me the right response: “If you are reading this, your application server is good to go!”
It seems that Glassfish couldn’t find the dataentry.html file, but I copy all code from the book and don't know what to do.
The code from the book didn't work because dataentry.html was created in the folder WEB-INF, I correct this mistake:
<welcome-file-list>
<welcome-file>/dataentry.html</welcome-file>
</welcome-file-list>
Now, I have my html file in the root directory. It works.

Categories

Resources