1

I wrote the below code to call a servlet from a JSP page

JSP code;

   <form method="post" action="${pageContext.request.contextPath}/Survey" name="_F_Surv">


         <button type="submit" name="button" value="button3">Button 3</button>

   </form>

Servlet Code

 package survey;

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

  import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 /**
 * Servlet implementation class Survey
  */
  @WebServlet("/Survey")
 public class Survey extends HttpServlet {
    private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public Survey() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
    PrintWriter out = response.getWriter();
    out.println("Hello World");
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

}

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" 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" id="WebApp_ID" version="2.5">
    <display-name>Struts2 Application</display-name>
   <servlet>
    <servlet-name>SimpleCaptcha</servlet-name>
   <servlet-class>nl.captcha.servlet.SimpleCaptchaServlet</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>SimpleCaptcha</servlet-name>
      <url-pattern>/simpleCaptcha.jpg</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Survey</servlet-name>
        <servlet-class>survey.Survey</servlet-class>
    </servlet>

    <servlet-mapping>
       <servlet-name>Survey</servlet-name>
       <url-pattern>/Survey</url-pattern>
    </servlet-mapping>


     <filter>
        <filter-name>struts2</filter-name>
         <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
         </filter-class>
      </filter>
      <filter-mapping>
           <filter-name>struts2</filter-name>
           <url-pattern>/*</url-pattern>
     </filter-mapping>
      <session-config>
          <session-timeout>1</session-timeout>
      </session-config>
      <welcome-file-list>
          <welcome-file>survey.jsp</welcome-file>
      </welcome-file-list>
      </web-app>

The Servlet is not getting called when I click the submit button. And I get the below error in the console

Jan 26, 2017 10:49:55 AM org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler error
SEVERE: Exception occurred during processing request: null
java.lang.NullPointerException
    at org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:557)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(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)

Can you please let me know what mistake I did here. Thanks in advance

  • 1
    What about `
    `?
    – Bruno Jan 26 '17 at 16:09
  • @BrunoDM The goal is likely to not use relative paths. – Gimby Jan 26 '17 at 16:12
  • As to the problem: on the page with the form, view the HTML source (or use the browser's DOM inspection development tools). What is the actual path that has been rendered into the form's action attribute? – Gimby Jan 26 '17 at 16:13
  • @Gimby THis is the actual path that has been rendered /Survey/Survey – krish mandava Jan 26 '17 at 16:20
  • 1
    You don't think it is important to mention you're using Struts 2 as well, and share how that is configured? Clearly struts is trying to take the request, so you will likely need some specific configuration to be able to invoke servlets without struts being mapped to the same url pattern. You probably have a web.xml, post that as well. – Gimby Jan 26 '17 at 16:25
  • @Gimby I added web.xml – krish mandava Jan 26 '17 at 16:32

1 Answers1

0

The problem is that you defined a filter to every URL hits Struts Dispachter. Take a look at struts2 ignore particular pattern and Can I exclude some concrete urls from <url-pattern> inside <filter-mapping>? to learn how to ignore a url in the filter. OR, change the filter to hit a URL like /struts/*, and change all your struts actions to this new pattern: /struts/{yourAction}.

Community
  • 1
  • 1
Bruno
  • 2,889
  • 1
  • 18
  • 25
  • Note: it is generally not recommended to expose the technology you're using to the client in any way. A security audit can and probably will shoot that down. /actions/* can already work. – Gimby Jan 31 '17 at 09:47