This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes HTTP Status 500 - --Only a type can be imported Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTTP Status 500 - --Only a type can be imported" Watch "HTTP Status 500 - --Only a type can be imported" New topic
Author

HTTP Status 500 - --Only a type can be imported

Abhilash Ravishankar
Greenhorn

Joined: Nov 16, 2011
Posts: 9
Index.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>Index</title>
</head>
<body>
<form method="get" action="loggin">
<h3>Enter the Vehicle ID</h3> <input type="text" name="vehicleID" />
<br>
<br>
<input type="submit" value="Submit"/>

</form>
</body>
</html>

logginServlet.java

package org.nissan.abhi;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nissan.abhi.dto.Cars;
import org.nissan.service.logginservice;



public class logginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException
{

try
{
doPost(req,res);
}
catch(IOException e)
{

}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


String vehicleID;

response.setContentType("text/html");

vehicleID= request.getParameter("vehicleID");
logginservice lgnsrv = new logginservice();
boolean result = lgnsrv.authenticate(vehicleID);



if(result)
{
Cars car2= lgnsrv.getCarDetails(vehicleID);
RequestDispatcher dispatcher = request.getRequestDispatcher("success.jsp");
dispatcher.forward(request, response);
return;
}
else
{
response.sendRedirect("index.jsp");
return;
}


}

}


logginservice.java

package org.nissan.service;

import java.util.HashMap;

import org.nissan.abhi.dto.Cars;

public class logginservice {

HashMap<String,String> cars = new HashMap<String,String>();

public logginservice()
{
cars.put("1234","Nissan");
cars.put("4567","Ford");
cars.put("8910","Dodge");
}


public boolean authenticate(String VehicleID){
if(VehicleID == null || VehicleID.trim()=="")
{
return false;
}
return true;
}

public Cars getCarDetails(String vehicleID)
{
Cars car1 = new Cars();
car1.setVehicleID(vehicleID);
car1.setCarName(cars.get(vehicleID));
return car1;

}
}


Success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="org.nissan.abhi.dto.Cars" %>
<!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>Success</title>
</head>
<body>
<h5>Loggin Successful</h5>
<%
Cars car =(Cars) request.getAttribute("cars");

%>
Vehicle ID : <%=car.getVehicleID()%>
Car Type : <%=car.getCarName()%>
</body>
</html>

Cars.Java

package org.nissan.abhi.dto;

public class Cars {

private String vehicleID;
private String carName;

public String getVehicleID() {
return vehicleID;
}
public void setVehicleID(String vehicleID) {
this.vehicleID = vehicleID;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}

}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>Nissan1</display-name>
<servlet>
<servlet-name>logginServlet</servlet-name>
<servlet-class>org.nissan.abhi.logginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logginServlet</servlet-name>
<url-pattern>/loggin</url-pattern>
</servlet-mapping>


<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

I’m getting the Error:
HTTP Status 500 -
________________________________________
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 6 in the generated java file
Only a type can be imported. org.nissan.abhi.dto.Cars resolves to a package

An error occurred at line: 12 in the jsp file: /success.jsp
Cars cannot be resolved to a type
9: <body>
10: <h5>Loggin Successful</h5>
11: <%
12: Cars car =(Cars) request.getAttribute("cars");
13:
14: %>
15: Vehicle ID : <%=car.getVehicleID()%>


An error occurred at line: 12 in the jsp file: /success.jsp
Cars cannot be resolved to a type
9: <body>
10: <h5>Loggin Successful</h5>
11: <%
12: Cars car =(Cars) request.getAttribute("cars");
13:
14: %>
15: Vehicle ID : <%=car.getVehicleID()%>


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717

Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
Double check to make sure the org.nissan.abhi.dto.Cars file is present under WEB-INF/classes directory.
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

Please use code tags when posting to forums, unformatted code are very difficult to read. First, if you are learning servlets/jsp, recommend you to start with good programming habits, i.e not to use any scriptlets in JSP, instead you should use JSTL.


Regards, Prasad
SCJP 5 (93%)
Abhilash Ravishankar
Greenhorn

Joined: Nov 16, 2011
Posts: 9
Hi Minawa,

Thanks for replying.The code worked fine.The org.nissan.abhi.dto.Cars is in Java Resources. I didn't understood the problem I'm experiencing.I feel there is some problem with the TomCat server.When ever I enter to debug mode and execute it and restart Eclipse, it stars working.Please help me to configure correctly the Tomcat Server with the eclipse or any changes need to be done in the settings.Thank you.
Tech Raj
Greenhorn

Joined: Nov 22, 2011
Posts: 2
i believe request.setAttribute("cars", car2); is needed, so that you can get the object in your JSp when you call request.getAttribute method.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Prasad Krishnegowda wrote:recommend you to start with good programming habits, i.e not to use any scriptlets in JSP, instead you should use JSTL.

Do not discount this advice -- it's very good advice. Java scriptlets in a JSP are outdated and discredited and a very poor practice.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Don't get me started about those stupid light bulbs.
 
subject: HTTP Status 500 - --Only a type can be imported
 
Similar Threads
DWR error
problem in Struts2 while running an application
WARNING: Could not find action or result
Struts2: No configuration found for the specified action
HTTP Status 405 - HTTP method GET is not supported by this URL