2

I am really struggling to figure out why I can't get to my servlet when submit my form. I have tried changing the path in the xml multiple times and edited the servlet code, but my browser still can't find my servlet when I submit the form.

Here is the web.xml:

<?xml version="1.0"?>
<web-app>


<servlet>
<servlet-name>quizServlet</servlet-name>
<servlet-class>quiz</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>quizServlet</servlet-name>
<url-pattern>/quiz</url-pattern>
</servlet-mapping>


</web-app>

I have also tried having form3/quiz be the url-pattern.

Here is the simple servlet:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

    public class quiz extends HttpServlet {
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        String grade = request.getParameter("grade");
        out.println("grade=" + grade);
        out.println("</body></html>");
        out.close();
    }

}

And here is the html:

<html>
<body>
Quiz
<form method='post' action='quiz'>
    grade<input type='text' name='grade'>
<input type='submit'></form>
</body>
</html>

index.html is contained in webapps/simpleForms/form3, web.xml is in webapps/simpleForms/form3/WEB-INF and quiz.java is in webapps/simpleForms/form3/WEB-INF/classes and is compiled.

Xerunix
  • 431
  • 1
  • 6
  • 21
  • What happens if you try to hit `http://localhost:8080/simpleForms/quiz` ? – Joakim Erdfelt Nov 05 '15 at 00:33
  • Your `web.xml` has no spec version or xsd/dtd specified, this can result in an undetermined servlet behavior mode. Your class names are also out of spec (encourage you to use a package AND a class that starts with a capitol letter). – Joakim Erdfelt Nov 05 '15 at 00:35

1 Answers1

0

your folder structure is having problem.I am able to run code provided by you.

  • if your project name is webapps,then keep your servlets in src(webapps/src/quiz) folder.Organize them using package if you want.
  • keep your html in WebContent folder(webapp/WebContent/yourHtml.html)
  • keep web.xml under WebContent/WEB-INF(webapp/WebContent/WEB-INF/web.xml)

With this folder structure,you should be able to run your servlet code.

RockAndRoll
  • 2,247
  • 2
  • 16
  • 35