0

So I created new maven module. I used "maven-archetype-webapp" to create it. And when I do normal text in index.jsp it works all ok. But I also wanted to do servlet.

I will shortly write how I crated it. I clicked right on my Module and New->Servlet. Java package: pl.marek, Class name: MyServlet and later I chose "doPost" and "doGet". And here is how my files look like:

index.jsp

<%@page import="java.util.Date"%>
<%@page import="pl.marek.countriesmapper.reader.*" %>
<%@page import="pl.marek.countriesmapper.preparer.*" %>
<%@page import="pl.marek.countriesmapper.country.*" %>
<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Hello</title>
</head>
<body>
<form action="MyServlet">
    Please enter your Username:  <input type="text" name="username" size="20px"> <br>
    Please enter your Password:  <input type="text" name="password" size="20px"> <br><br>
<input type="submit" value="submit">
</form>
</body>
</html>

MyServlet.java

package pl.marek;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServlet
 */
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        PrintWriter out = response.getWriter();
        out.println (
                  "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" +" +
                      "http://www.w3.org/TR/html4/loose.dtd\">\n" +
                  "<html> \n" +
                    "<head> \n" +
                      "<meta http-equiv=\"Content-Type\" content=\"text/html; " +
                        "charset=ISO-8859-1\"> \n" +
                      "<title> Crunchify.com JSP Servlet Example  </title> \n" +
                    "</head> \n" +
                    "<body> <div align='center'> \n" +
                      "<style= \"font-size=\"12px\" color='black'\"" + "\">" +
                        "Username: " + username + " <br> " +
                        "Password: " + password +
                    "</font></body> \n" +
                  "</html>"
                );     
        }
    }
    /**
     * @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

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
      <servlet-name>MyServlet</servlet-name>
      <display-name>MyServlet</display-name>
      <description></description>
      <servlet-class>pl.marek.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>MyServlet</servlet-name>
      <url-pattern>MyServlet</url-pattern>
  </servlet-mapping>
</web-app>

And here is photo of my project:

enter image description here

And when I click submit I have adress: localhost:8080/WebAplication/MyServlet?username=test123&password=test321

But on webpage I have:

HTTP Status 404 -

type Status report

message

description The requested resource is not available.
Apache Tomcat/7.0.47

Somebody maybe have idea what I'm doing wrong? :/

@edit

I deleted "/" in web.xml and now I have this:

HTTP Status 500 - Error instantiating servlet class pl.marek.MyServlet

Error 500 solved, but I still have status 404 :/

@edit2

I made what wrote SaviNuclear and I have still 404 error :/

@edit3

Problem solved. I had to create src/java instead of src/resources.

Marek S
  • 109
  • 2
  • 10

2 Answers2

0

In your form action prepend / so that it changes to /MyServlet.

Rahul Yadav
  • 1,503
  • 8
  • 11
0

In your servlet there is a error so please delete a closing bracket } at line number 44 and re compile your servlet to get the proper output. enter image description here

SaviNuclear
  • 886
  • 1
  • 7
  • 19