0

** My Problem is when i run my index.jsp every time i got this error in my showAlien.jsp file and how to solve this **

enter image description here

CODE

index/jsp

<html>
    <body>
        <h2>Hello World!</h2>
        <form action="getAlien">
            <input type="text" name="aid">
            <input type="submit">
        </form>
    </body>
</html>

GetAlienController

import java.io.IOException;
import javax.servlet.RequestDispatcher;
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("/getAlien")
public class GetAlienController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int aid = Integer.parseInt(request.getParameter("aid"));
        AlienDao dao = new AlienDao();
        Alien a1 = dao.getAlien(aid);
        
        request.setAttribute("alien", a1);
        RequestDispatcher rd = request.getRequestDispatcher("showAlien.jsp");
        rd.forward(request, response);
        }
}

Alien.java

public class Alien {
    private int aid;
    private String aname;
    private String tech;
    public int getAid() {
        return aid;
    }
    public void setAid(int aid) {
        this.aid = aid;
    }
    public String getAname() {
        return aname;
    }
    public void setAname(String aname) {
        this.aname = aname;
    }
    public String getTech() {
        return tech;
    }
    public void setTech(String tech) {
        this.tech = tech;
    }
    @Override
    public String toString() {
        return "Alien [aid=" + aid + ", aname=" + aname + ", tech=" + tech + "]";
    }
}

AlienDao.java

public class AlienDao {
    public Alien getAlien(int aid) {
        Alien a = new Alien();
        a.setAid(101);
        a.setAname("ss");
        a.setTech("java");
        return a;
    }
}

showAlien

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>ShowAliens....</title>
    </head>
    <body bgcolor="cyan">
        <%
            Alien a1 = (Alien)request.getAttribute("alien");
            out.println("Hello");
        %>
    </body>
</html> 

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" 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>M</display-name>
  <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>

Is something wrong with my web.xml file Sorry for posting the whole code but i am unable to tell my whole problem.

Community
  • 1
  • 1
Akash
  • 425
  • 2
  • 7
  • 21
  • 3
    Your JSP needs to know what `Alien` is. Either use the full classname (including the package) or import it properlly inyour JSP. – M. Deinum Dec 02 '19 at 12:57
  • @M. Deinum sir actually I am not created any package all these files are stored in the default package – Akash Dec 02 '19 at 13:02
  • 4
    Do not use default package - although you can, it is highly recommended **not** to do so (see https://stackoverflow.com/a/7849460/3511123). And don't forget to deploy the compiled classes to the `WEB-INF/classes` of you web application (or to `WEB-INF/lib` if you have them as a JAR file). – Jozef Chocholacek Dec 02 '19 at 13:12
  • @JozefChocholacek sir what I need to do to move all classes into separate package and in my showAlien. jsp i need to import them. ? – Akash Dec 02 '19 at 13:15
  • 1
    Right-click on the source file and use *Refactor* -> *Move* to put it in a package. You can then either write a `@page` directive to import the class, or use content assist to complete the type name in the scriptlet source and have it added for you. – nitind Dec 02 '19 at 13:17
  • 1
    Stop using scriptlets `<% ... %>`. Just use EL `${...}`. In your case, the alien is available as `${alien}`. Then you don't need to explicitly import classes in JSP. – BalusC Dec 02 '19 at 14:35

0 Answers0