0

I am getting blank page as output in my web browser when I run DemoServlet

note : I have used both doGet and doPost methods in my servlet instead of service method even though I am getting the same output.

The below is the following code

public class Student_Class {

    int RollNo = 0;
    String Name="";
    
    Student_Class(int RollNo,String Name) {
        this.RollNo = RollNo;
        this.Name = Name;
    }
    
    public int getRollNo() {
        return RollNo;
    }
    
    public String getName() {
        return Name;
    }
    
}

the above is my StudentClass

I have created a servlet like below

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

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

public class DemoServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        Student_Class Student = new Student_Class(1,"Name1");
        request.setAttribute("Student", Student);
        RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
        rd.forward(request, response);
        
    }
}

and my jsp file is below the name of my jsp file is : display.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>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
        
    ${Student}
</body>
</html>

my web.xml file is like below

<?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/j2ee" xmlns:web="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_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.5">
  <display-name>JSTL_Demo</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>
  <servlet>
    <description></description>
    <display-name>Demo</display-name>
    <servlet-name>Demo</servlet-name>
    <servlet-class>Demo</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Demo</servlet-name>
    <url-pattern>/Demo</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>DemoServlet</display-name>
    <servlet-name>DemoServlet</servlet-name>
    <servlet-class>DemoServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DemoServlet</servlet-name>
    <url-pattern>/DemoServlet</url-pattern>
  </servlet-mapping>
</web-app>

I am using tomcat 10.0.10 as my server and eclipse as my IDE

please help me I am a beginner and I don't know much about jsp and servlets correct me if I have done anything wrong

click here to see the output on my browser

click here to see the project explorer

firstuser
  • 9
  • 5
  • What is location of display.jsp? – LHA Oct 05 '21 at 19:52
  • Define 'I am getting null'. – user207421 Oct 05 '21 at 23:50
  • When doing `request.setAttribute("xxx", ...)`, access it as `${requestScope.xxx}` or `${requestScope['xxx']}` - [see here too](https://stackoverflow.com/questions/4912690/how-to-access-at-request-attributes-in-jsp). – Nikos Paraskevopoulos Oct 06 '21 at 07:51
  • @user207421 null in the sense I am getting a blank page as my output – firstuser Oct 06 '21 at 08:03
  • @NikosParaskevopoulos thankyou for your suggestion but still I am getting a blank page as the output I have attached a image in my question please check it – firstuser Oct 06 '21 at 08:05
  • @Loc for your reference I have attached an image of the project explorer hope you find it helpful – firstuser Oct 06 '21 at 08:10
  • Try /display.jsp and see if it work. OR put display.jsp into WEB-INF folder then try /WEB-INF/display.jsp – LHA Oct 06 '21 at 12:31
  • @Loc I am getting the following message : JSP file [/display.jsp] not found. when I kept the display.jsp into WEB-INF folder – firstuser Oct 06 '21 at 13:47
  • If you put display.jsp into WEB-INF, you have to use this path: /WEB-INF/display.jsp – LHA Oct 06 '21 at 13:52
  • Don't touch service method, use doGet instead – LHA Oct 06 '21 at 13:53
  • @Loc actually I want to forward the request and response to display.jsp from DemoServlet using Request Dispatcher and I want to pass the Student object from DemoServlet to display.jsp file and want to print the hashcode of the Student object in the web browser which was passed by the DemoServlet to display.jsp – firstuser Oct 06 '21 at 13:57
  • @Loc thanks for your suggestion to use doGet method even though I am using doGet method I am getting the blank page – firstuser Oct 06 '21 at 14:03
  • @Loc yes I am using /WEB-INF/display.jsp path when I use this path I am getting 404 error BTW thanks for your time – firstuser Oct 06 '21 at 14:04

0 Answers0