There is a jsp page - home.jsp. There are two blocks: one adds the data to the database (it works), and the second gets the data from the database and displays it as a tile (ul -> li). It must receive data upon authorization and after adding new data in the first block.

When I log into my account I wrote this error (redirection works (:

SEVERE: Servlet.service() for servlet [Home] in context with path [/Site] threw exception [An exception occurred processing JSP page /home.jsp at line 70 67: <% 68: ArrayList<Link> links = (ArrayList<Link>)request.getAttribute("links"); 69: 70: for(Link link: links) 71: { 72: System.out.println(link.link.toString()); 73: //out.println(link.link.toString()); Stacktrace:] with root cause java.lang.NullPointerException at org.apache.jsp.home_jsp._jspService(home_jsp.java:199) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source) 

I can't understand why this happens. Checked in the debugger and the data in the ArrayList links is the data in the Link link too.

home.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String login = null; Cookie[] cookies = request.getCookies(); for(Cookie cookie: cookies) { //out.println(cookies.length); if(cookie.getName().equals("login")) { login = cookie.getValue(); } } if(login == null) { response.sendRedirect("index"); } request.setAttribute("login", login); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>YOUR LINKS - Home</title> <link rel="stylesheet" href="css/normalize.css"> <link rel="stylesheet" href="css/style.css"> <link href='https://fonts.googleapis.com/css?family=Open+Sans&subset=latin,cyrillic,cyrillic-ext' rel='stylesheet' type='text/css'> <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:600,700&subset=latin,vietnamese,latin-ext' rel='stylesheet' type='text/css'> <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css'> </head> <body> <div id="wrapper"> <div id="content"> <%@include file="header.jsp" %> <form action="Logout" method="post"><button type="submit" class="logoutBtn">Logout</button></form> <div class="addLink"> <form action="AddLink" method="post"> <div class="topForm"> <span id="Info"> <% Object infoHome = request.getAttribute("InfoHome"); if (infoHome != null) { out.append(infoHome.toString()); } %> </span> <p class="linkP">Link<span class="req">*</span></p> <input type="text" required placeholder="Paste your link" name="link"/> <p class="titleP" style="width: 4%; margin-left: 35px;">Title</p> <input style="width: 22%;" type="text" placeholder="Title" name="title"/> </div> <p class="descrP">Description</p> <textarea rows="4" cols="10" placeholder="Enter description for a link" name="descr"></textarea> <button type="submit" class="buttonAdd">Add Link</button> </form> </div> <div class="links"> <p>Saved links</p> <div class="bar"> <ul> <%@ page import="java.*" %> <%@ page import="java.util.*" %> <%@ page import="ru.tsyklop.site.Link" %> <% ArrayList<Link> links = (ArrayList<Link>)request.getAttribute("links"); for(Link link: links) { System.out.println(link.link.toString()); //out.println(link.link.toString()); %> <li class="link"><a href="<%= link.link.toString() %>"><%= link.title.toString() %></a></li> <% } %> <%--<c:forEach items="${links}" var="current"> <li class="link"><a href="${current.link}">${current.title}</a></li> </c:forEach> --%> <!--<li class="link"><a>Title</a></li> <li class="link"><a>Title</a></li> <li class="link"><a>Title</a></li> <li class="link"><a>Title</a></li> <li class="link"><a>Title</a></li> <li class="link"><a>Title</a></li> <li class="link"><a>Title</a></li>--> </ul> </div> </div> <%@include file="footer.jsp" %> </div> </div> </body> </html> 

    1 answer 1

    Identified problem: During the first authorization, we receive data from the database. But the cookies that should be created during authorization were not created. In home.jsp there is a check for cookies - accordingly, the check did not pass as there are no cookies. We were redirected to index.jsp. And then the cookies appeared and redirects us to home.jsp but we no longer have the data that we got the first time. Here is a mistake.

    Decision:

     Cookie cookie = new Cookie("login", Login); cookie.setMaxAge(60*20); response.addCookie(cookie); cookie.setPath("/"); cookie.setDomain("localhost"); request.setAttribute("links", connect.linkArr); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/home"); dispatcher.include(request,response); 

    In the authorization file, where we create cookies, add cookie.setPath ("/"); cookie.setDomain ("localhost"); after the response.addCookie line (cookie); I. We change the dispatcher. It works for me!

    • it works only at the first authorization! If you close the tab and open it will write an error! Help is needed! - Tsyklop