Gentle Breeze
[Servlet] Request Dispatcher 본문
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import data.*;
import business.*;
public class EmailServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public EmailServlet()
{
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//Processing businuss logic
String first = request.getParameter("firstName");
String last = request.getParameter("lastName");
String email = request.getParameter("emailAddress");
User user = new User(first, last, email);
UserIO.addRecord(user, "C:\\UserEmail.txt");
//Request Dispatch
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/email6/show_email_list.jsp");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
}
===================================================================
* business/User.java
package business;
public class User
{
private String firstName;
private String lastName;
private String emailAddress;
public User(){}
public User(String first, String last, String email)
{
firstName = first;
lastName = last;
emailAddress = email;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getEmailAddress()
{
return emailAddress;
}
public void setEmailAddress(String emailAddress)
{
this.emailAddress = emailAddress;
}
}
===================================================================
* data/UserIO.java
package data;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import business.User;
public class UserIO {
public static synchronized
void addRecord( User user, String fileName) throws IOException
{
PrintWriter out = new PrintWriter(new FileWriter(fileName,true));
out.println( user.getEmailAddress() + "|" +
user.getFirstName() + "|" +
user.getLastName() );
out.close();
}
}
===================================================================
* join_email_entry.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action = "/WebDay06/servlet/EmailServlet6" method = post>
first name : <input type = text name = firstName><br>
last name : <input type = text name = lastName><br>
email address : <input type = text name = emailAddress><br>
<input type = submit name = submit><br>
</form>
</body>
</html>
===================================================================
* show_email_list.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("KSC5601");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
Entered First Name : <%= firstName %><br>
Entered Last Name : <%= lastName %><br>
Entered Email Address : <%= emailAddress %>
</body>
</html>
'⑥ PreSTC > jsp / Servlet' 카테고리의 다른 글
[Servlet] web.xml <2> (0) | 2008.07.23 |
---|---|
[jsp] include (0) | 2008.07.23 |
[jsp] JSP에서 인스턴스 변수 사용하기 (0) | 2008.07.23 |
[jsp/Servlet] Request Dispatch vs. Send Redirect (0) | 2008.07.23 |
[Servlet] web.xml <1> (0) | 2008.07.22 |