This is an simple approach to dispatch a value to index.jsp
index.html:
<html>
<body>
<h1 align="center">Select Person</h1>
<form method="POST" action="Person.do">
<select name="person" size="1">
<option value="a@example.com">Fred</option>
</select>
<center><input type="Submit"></center>
</form>
</body></html>
result.jsp:
<p>
<%
// request email from servlet
String email = (String)request.getAttribute("email");
// print email
out.println(""+email);
%>
</p>
Person.java:
package com.example;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Person extends HttpServlet
{
public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
String p = request.getParameter("person");
// display email stored in web.xml
PrintWriter out = response.getWriter();
request.setAttribute("email", p);
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}
}
web.xml (stored inside WEB-INF):
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Person Servlet Example</display-name>
<servlet>
<servlet-name>Person</servlet-name>
<servlet-class>com.example.Person</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Person</servlet-name>
<url-pattern>/Person.do</url-pattern>
</servlet-mapping>
</web-app> martin5211
Posting Whiz in Training
271 posts since Aug 2007
Reputation Points: 52
Solved Threads: 23