0

For some reasons my servlet doGet method is being called twice. I am calling servlet from the index.jsp page by using servlet annotation "/Profile/*. And it passes path as localhost:1919/Profile/username to getPost method.

<li><a href="Profile/<%=lg.getUsername()%>">Your Profile</a></li>

Servlet Profile retrieves data from the java container stores it as an attribute and forwards to profile.jsp page.

rd = request.getRequestDispatcher("/profile.jsp");
            request.setAttribute("ProfileInfo", proInfo);

            rd.forward(request, response);

My profile.jsp page uses java code to show all the data (and it shows it perfectly). But then I click link to the next update_profile.jsp page. Just simple link. Then I realize that on this step for strange reasons for me doGet method from Profile servlet is called the second time. And passes path as localhost:1919/Profile/update_profile.jsp

Here is my profile.jsp code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="uk.ac.dundee.computing.aec.instagrim.containers.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Picturize - User Profile</title>
   <!-- <link rel="stylesheet" type="text/css" href="Styles.css"/> -->
</head>
<body>

<header>
    <h2>Picturize - Profile of user</h2>
</header>
<%
    ProfileInfo proInfo = (ProfileInfo) request.getAttribute("ProfileInfo");
%>
<article>

    <h2>User: <%=proInfo.getUsername()%>
    </h2>

    <h3>First name: <%=proInfo.getFirstname()%>
    </h3>

    <h3>Last name: <%=proInfo.getLastname()%>
    </h3>

    <h3>Email address: <%=proInfo.getEmail()%>
    </h3>

    <a href="update_profile.jsp">Update your profile information</a>
</article>
</body>
</html>

And update_profile.jsp file is just simple file with <h1>hello</h1> output.

I have looked in the internet that this might be a mapping problem or my css relative path connection problem. As you see I have commented out my css connection and problem still exists.

My run output in the IDE also shows these errors:

org.apache.jasper.JasperException: An exception occurred processing JSP page /profile.jsp at line 27

24: %>
25: <article>
26: 
27:     <h2>User: <%=proInfo.getUsername()%>
28:     </h2>
29: 
30:     <h3>First name: <%=proInfo.getFirstname()%>


Stacktrace:
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:335)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
    at uk.ac.dundee.computing.aec.instagrim.servlets.Profile.displayProfile(Profile.java:116)
    at uk.ac.dundee.computing.aec.instagrim.servlets.Profile.doGet(Profile.java:68)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
    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)
Caused by: java.lang.NullPointerException
    at org.apache.jsp.profile_jsp._jspService(profile_jsp.java:91)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    ... 38 more

Here is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="2.4">
    <description>MySQL Test App</description>

    <servlet>
        <servlet-name>Profile</servlet-name>
        <servlet-class>uk.ac.dundee.computing.aec.instagrim.servlets.Profile</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Profile</servlet-name>
        <url-pattern>/profile</url-pattern>
    </servlet-mapping>

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>picturizedb</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

And my tomcat run configuration settings: https://drive.google.com/file/d/0ByZXQ2R3O8B8SERPYzJxZFViLVE/view?usp=sharing and https://drive.google.com/file/d/0ByZXQ2R3O8B8THE3TGFnUFVpNjg/view?usp=sharing

Thank you very much for your help in advance!

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
code4fun
  • 35
  • 1
  • 8

1 Answers1

1

OK, so the problem has to do with the way you mapped your servlet and the way you want to access your pages.

So your root url is: http://localhost:1919/

When you first open profile (using this code <a href="Profile/<%=lg.getUsername()%>">) browser makes an access to http://localhost:1919/Profile/xyz. This call invokes the servlet, since it is mapped to parse all requests starting with /Profile.Till now everything is working like you expect.

At this point your url in browser looks like: http://localhost:1919/Profile/xyz. Now, you click on the link to update the user and the browser appends update_profile.jsp to http://localhost:1919/Profile/, so the url looks like http://localhost:1919/Profile/update_profile.jsp, but the problem is, it still begins with /Profile and still fits the servlet mapping you have defined in web.xml.. So the servlet is obviously invoked..

Solutions:
1) Define in web.xml a new servlet, with more specific url, which will handle your update request(check more about servlet url pattern matching here).
2) In case there is no logical relation between Profile path and update_profile.jsp (which looks to me unlikely from the code logic) you could use / in href (something like <a href="/update_profile.jsp">) which will make browser to make a request to http://localhost:1919/update_profile.jsp instead of http://localhost:1919/Profile/update_profile.jsp.

EDIT: here is also nice example of how the / in urls should be used.

Community
  • 1
  • 1
Serhiy
  • 4,073
  • 3
  • 36
  • 66