Servlets
Hi,
I am trying to create a servlet that will take an input from the user in the form of a button press.
I want an already created html page to be displayed to the user when the servlet is created and capture the button press in order to run some code.
What I need to know is how do I do this, do I specify the location and name of the html page in the doGet method? I also wanted to update the html page with the code that was being run but I am not sure if I can do this. Would I need to create another html page in the doPost method to display this code? Would this be the easiest option?
Sorry I am new to servlets and only have a limited time to create this.
Thank you.
[nobr]I didn't understand what do you want exactly.
Here a simple example to start with :
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>web.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>register.jsp</welcome-file>
</welcome-file-list>
</web-app>
registerServlet.java
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
System.out.println("name="+name);
System.out.println("email="+email);
//do a business task here : data processing, db access...
request.getRequestDispatcher("/registered.jsp").forward(request, response);
//response.sendRedirect("/page.jsp");
}
}
register.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/register">
Name: <input name="name" value=""></input><br:>
Email: <input name="email" value=""></input>
</form>
</body>
</html>
registered.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Name = <%= request.getParameter("name")%><br/>
Email = <%= request.getParameter("email")%>
</body>
</html>
compile the servlet and create/deploy the application war in a app server (tomcat for example).
Use http://127.0.0.1:8080/YOUR_WAR_NAME
hope that helps[/nobr]