skiabox
skiabox

Reputation: 3507

Post request is not working in my servlet but get request does

I have created a servlet that is capable of responding to both get and post requests. The problem is that when I am sending a post request using a form I get no response. Here is the code.

XmlServlet.java :

package org.skiabox.myservlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class XmlServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String fullName = request.getParameter("fullName");
        String userName = request.getParameter("userName");
        out.println("Hello from POST method " + userName + "! We know your full name is " + fullName);

        String prof = request.getParameter("prof");
        out.println("<br/>" + "You are a " + prof);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String userName = request.getParameter("userName");
        out.println("Hello " + userName);
    }
}

SimpleForm.html :

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form method="post" action="/XmlServletPath">
        User Name:<input name="userName"/>
        Full Name:<input name="fullName"/>
        <br/>
        <br/>
        Profession:
        <input type="radio" name="prof" value="Developer">Developer</input>
        <input type="radio" name="prof" value="Architect">Architect</input>
        <input type="submit"/>
    </form>
</body>
</html>

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <description>A simple servlet</description>
        <display-name>SimpleServlet</display-name>
        <servlet-name>SimpleServlet</servlet-name>
        <servlet-class>org.skiabox.myservlet.SimpleServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>XmlServlet</servlet-name>
        <servlet-class>org.skiabox.myservlet.XmlServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>SimpleServlet</servlet-name>
        <url-pattern>/SimpleServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>XmlServlet</servlet-name>
        <url-pattern>/XmlServletPath</url-pattern>
    </servlet-mapping>
</web-app>

SimpleServletProject.iml :

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="web" name="Web">
      <configuration>
        <descriptors>
          <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
        </descriptors>
        <webroots>
          <root url="file://$MODULE_DIR$/web" relative="/" />
        </webroots>
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" scope="PROVIDED" name="Tomcat 7.0" level="application_server_libraries" />
  </component>
</module>

Tomcat settings : picture1

picture2

Page to enter details url : http://localhost:8080/SimpleServletProject/SimpleForm.html

Result page (completely empty) url after pressing submit : http://localhost:8080/XmlServletPath

Upvotes: 1

Views: 6347

Answers (1)

BalusC
BalusC

Reputation: 1108632

Your servlet is listening on http://localhost:8080/SimpleServletProject/XmlServletPath, not on http://localhost:8080/XmlServletPath. Your form was submitting to the wrong URL.

Just fix the form action URL accordingly. Replace

<form method="post" action="/XmlServletPath">

by

<form method="post" action="/SimpleServletProject/XmlServletPath">

or, if the page presenting the form is in the same URL folder anyway

<form method="post" action="XmlServletPath">

or, if you worry about the dynamicness of the context path

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

Upvotes: 3

Related Questions