Actually i'm trying to display the details obtained from JSP form with servlet. But I'm not able to display the JSP page. But I can see the program entering into the POST method in Servlet.
Here is my code,
Startup.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="controlServlets" method="post">
<input type="text" name="name"/><br>
<input type="text" name="group"/>
<input type="text" name="pass"/>
<input type="submit" value="submit">
</form>
</body>
</html>
web.xml
<web-app>
<servlet>
<servlet-name>controlServlets</servlet-name>
<servlet-class>com.selenium8x8.servlet.ControlServlets</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controlServlets</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
ControlServlets.java
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ControlServlets")
public class ControlServlets extends HttpServlet {
private static final long serialVersionUID = 1L;
public ControlServlets() {
super();
// TODO Auto-generated constructor stub
}
// @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String group = request.getParameter("group");
String pass = request.getParameter("pass");
System.out.println("Name :"+ name);
System.out.println("group :"+ group);
System.out.println("pass :"+ pass);
System.out.println("Post method");
}
}
In console,
I can see the following,
Name :null
group :null
pass :null
Post method
Please Help...
Part I)If you want to use web.xml
for your application then you need to make following changes :
1)In Startup.jsp
change the action
attribute of <form>
tag to
<form action="ControlServlets" method="post">
↑
2)In web.xml
change the <servlet-mapping>
to
<servlet-mapping>
<servlet-name>controlServlets</servlet-name>
<url-pattern>/ControlServlets</url-pattern>
</servlet-mapping>
3)In ControlServlets.java
several changes as, in web.xml
you mentioned
<servlet-class>com.selenium8x8.servlet.ControlServlets</servlet-class>
↑
This is the package name, so you must have first statement in ControlServlets.java
package com.selenium8x8.servlet; //in your code it is missing
Then, comment following two lines
//import javax.servlet.annotation.WebServlet;
and
//@WebServlet("/ControlServlets")
Now, run application, it will give you desired output.
Part II) If you want to use @WebServlet annotation, as you did
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ControlServlets")
public class ControlServlets extends HttpServlet {
...
.....
.......
}
Then, no need for web.xml
. The above does basically the same as following:
<servlet>
<servlet-name>controlServlets</servlet-name>
<servlet-class>com.selenium8x8.servlet.ControlServlets</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controlServlets</servlet-name>
<url-pattern>/ControlServlets</url-pattern>
</servlet-mapping>
For using @WebServlet
annotation you need Java EE 6 / Servlet 3.0