Issue
I’m reading the book "Head first servlets & JSP
" and using tomcat 9
, jdk8
,eclipse neon
.The servlet
is not working.I am getting an error:"HTTP Status 500 - Error instantiating servlet class com.example.web.BeerSelect
"
The picture is my workspace.
this is my HTML
code.(form.html
)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>beer</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>
<option value="dark">dark</option>
</select>
<br><br>
<center>
<input type="SUBMIT">
</center>
</form>
</body>
</html>
this is my 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">
<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</web-app>
This is my servlet
(BeerSelect.java
)
package com.example.web;
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;
@SuppressWarnings("serial")
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);
}
}
.class
is not generated.What's wrong with it.
Solution
Hmm I can't see the .class
file in your workspace. This is just a suggestion but you could try compiling the servlet
java file by using
javac BeerSelect.java
This should create a BeerSelect.class
file. Then create a classes
folder and put it in there. Like this:
WebContent/WEB-INF/classes/com/example/web/BeerSelect.class
See if that helps at all.
Answered By - Alan