-1

This is my html code:

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

    <label for="semester">Semester</label>
    <input type="text" name="semester" placeholder="Enter the semester">

    <label for="prof-name">Instructor's name</label>
    <input type="text" name="prof-name" placeholder="Enter the instructor's name">

    <label id="course-eval">How was the course?</label>
    <textarea  name="course-evaluation" placeholder="Write everything about the course"></textarea>

    <label for="prof-eval">How was your professor?</label>
    <textarea name="prof-evaluation" placeholder="Write everything about your professor"></textarea>

    <input type="submit" value="Submit" >

</form>

And this is my servlet code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String semester = request.getParameter("semester");
    String name = request.getParameter("prof-name");
    String course = request.getParameter("course-evaluation");
    String prof = request.getParameter("prof-evaluation");

    response.setContentType("text/html;");
    PrintWriter writer = response.getWriter();

    writer.println("<html><head></head><body>");
    writer.println("Semester:"+semester+"<br/>");
    writer.println("Instructor's name:"+name+"<br/>");
    writer.println("How was the course?:"+course+"<br/>");
    writer.println("How was your professor?:"+prof+"<br/>");
    writer.print("</body></html>");
    writer.close();
}

This is my 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"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>skce</display-name>
  <welcome-file-list>
    <welcome-file>cse101.html</welcome-file>
    <welcome-file>cse101.htm</welcome-file>
    <welcome-file>cse101.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
   <servlet-name>Post</servlet-name>
   <servlet-class>com.skce.post.Post</servlet-class>

   <init-param>
    <param-name>semester</param-name>
    <param-value></param-value>
   </init-param>

   <init-param>
    <param-name>prof-name</param-name>
    <param-value></param-value>
   </init-param>

   <init-param>
    <param-name>course-evaluation</param-name>
    <param-value></param-value>
   </init-param>

   <init-param>
    <param-name>prof-evaluation</param-name>
    <param-value></param-value>
   </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>Post</servlet-name>
    <url-pattern>/post</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>com.skce.post.ContextListener</listener-class>
  </listener>
</web-app>

And I got HTTP Status 404 and description is "The requested resource is not available." I use Apache Tomcat/7.0.73.

What could make this error?

Doeun Kim
  • 23
  • 8

4 Answers4

-1
<form action='post' method="POST">
    ....
</form>
Smember
  • 49
  • 1
  • 1
  • 5
-1

I suspect your 'action' attribute. Is 'Post' your servlet name? Try giving your complete url like '/application-name/servletName'. Also as mentioned in the comments, make sure of your servlet-mapping in web.xml

liwevire
  • 782
  • 2
  • 9
  • 21
  • what do you mean by application name? do you mean package name? Does my web.xml seem right? – Doeun Kim Jan 22 '17 at 06:20
  • hello Doeun, your application name is skce(as mentioned in your web.xml), URL pattern for 'Post' is '/post'(again from web.xml), try the following, in your action attribute, **action="/skce/post"**, if fails try give this one a try **action="/skce/post/"** – liwevire Jan 22 '17 at 10:39
  • You can also try the answer given by @W-S also which basically does what I have mentioned and I recommend his way. My way is kinda hardcoding. – liwevire Jan 22 '17 at 10:45
-1

Change

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

to

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

Why?

In your current code, you haven't specified the action attribute on the form. So your form post defaults to "self" meaning that the post request will hit your servlet container with the URL of your form itself. However you don't have a post handler servlet mapped to the form url. So Catalina throws 404 - client error. I see that your post handler servlet is mapped to the url patter of "/post". Therefore, in order to make this form submit work, you must use "/post" in your form action.

VHS
  • 9,534
  • 3
  • 19
  • 43
  • didn't solve... – Doeun Kim Jan 22 '17 at 06:19
  • @DoeunKim. Try `action = "post"` (no forward slash). – VHS Jan 22 '17 at 06:22
  • If it still doesn't work, let me know the complete form URL and how you render your form. – VHS Jan 22 '17 at 06:23
  • didn't work. where can I find the url? or can I just send my folder to you? – Doeun Kim Jan 22 '17 at 06:25
  • The form URL as it appears in your browser. Also, how did your server render the form? Is it a simple servlet render? – VHS Jan 22 '17 at 06:26
  • do you mean by http://localhost:8181/skce/Post? I'm using eclipse to render the server. – Doeun Kim Jan 22 '17 at 06:35
  • If your application context path is `skce`, the action of your form should be `/skce/post`. – W-S Jan 22 '17 at 06:38
  • @W-S didn't work. Will you import my project? I'll send my project to you. – Doeun Kim Jan 22 '17 at 06:41
  • oh! it worked! I put
    . Thanks guys and especially @W-S
    – Doeun Kim Jan 22 '17 at 06:44
  • And also one thing. should I put init-param blocks? – Doeun Kim Jan 22 '17 at 06:45
  • I think you don't need those `init-param`. One more thing, to link your label with the form input the `for` attribute in the label should be match with the `id` attribute on the input / textarea element. – W-S Jan 22 '17 at 06:50
  • @DoeunKim, you should upvote the answers that helped you solve your problems. In addition, you should also pick one of the answers here and mark it as 'answer' for future developers who face this problem and look for a solution – VHS Jan 22 '17 at 06:53
-1

If your form is a JSP page, use taglib to generate the correct context path for your form, so you don't hard-coded it in your form action attribute. The <c:url/> taglib adds the correct context path to your form action.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<form action="<c:url value="/post"/>" method="post"  >
...
</form>

To use the taglib you need to add the following dependency to your web project.

<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
W-S
  • 516
  • 5
  • 14