I have a simple login.jsp
and loginservlet
that check users, if username is correct then open Main.jsp
page.
login.jsp
is my default page when run my webapp.
Main.jsp
has 2 tabs, the second tab use DocTab.jsp
as iframe.
DocTab.jsp
has a table that fill by a list from MainServlet
.
When I use http://localhost:8080/MyWebApp/MainServlet
the DocTab.jsp
is displayed and table is filled, but when I run webapp after username and password checking in login.jsp
the Main.jsp
is running but second tab (DocTab.jsp) displayed by this error:
org.apache.jasper.JasperException: java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "dataList" is null
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
<form action="LoginServlet" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
loginServlet
package com.example.simplewebapp;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("1") && password.equals("1")) {
response.sendRedirect("Main.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
}
Main.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Main</title>
</head>
<body>
<h1>Welcome to the Main Page!</h1>
<ul>
<li><a href="#tab1">Tab 1</a></li>
<li><a href="#tab2">Document Tab</a></li>
</ul>
<div id="tab1">
<p>Content for Tab 1.</p>
</div>
<div id="tab2">
<iframe src="DocTab.jsp"></iframe>
</div>
</body>
</html>
MainServlet
package com.example.simplewebapp;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/MainServlet")
public class MainServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<String> dataList = new ArrayList<String>();
dataList.add("Data 1");
dataList.add("Data 2");
dataList.add("Data 3");
dataList.add("Data 4");
dataList.add("Data 5");
request.setAttribute("dataList", dataList);
request.getRequestDispatcher("DocTab.jsp").forward(request, response);
}
}
DocTab.jsp
<%@ page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Document Tab</title>
</head>
<body>
<table border=1 >
<tr><th>Column 1</th><th>Column 2</th></tr>
<% List<String> dataList = (List<String>) request.getAttribute("dataList");
for(int i=0; i<dataList.size(); i++) { %>
<tr><td><%=dataList.get(i)%></td><td>Row <%=i+1%>, Column 2</td></tr>
<% } %>
</table>
</body>
</html>