小方的Java博客

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  27 随笔 :: 17 文章 :: 115 评论 :: 0 Trackbacks

<web-app>
 <servlet>
  <servlet-name>FirstServlet</servlet-name>
  <servlet-class>servlet.study3.c1.FirstServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>FirstServlet</servlet-name>
  <url-pattern>/firstservlet</url-pattern>
 </servlet-mapping>
</web-app>

package servlet.study3.c1;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;

public class FirstServlet extends HttpServlet{
    
public void doGet(HttpServletRequest request, HttpServletResponse response)
        
throws ServletException,IOException{
        response.setContentType("text/html");
        PrintWriter out 
= response.getWriter();
        out.println(
"<html><head>");
        out.println(
"<title>Help Page</title></head><body>");
        out.println(
"<h2>Please submit your information</h2>");
        out.println(
"<form method='post' action='/jsp/firstservlet'>");
          out.println(
"<table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
        out.println(
"<tr>");
        out.println(
"<td>Your First name: </td>");
        out.println(
"<td><input type=\"text\" name=\"firstname\"></td>");
        out.println(
"</tr>");
        out.println(
"<tr>");
        out.println(
"<td>Your Last name:</td>");
        out.println(
"<td><input type=\"text\" name=\"lastname\"></td>");
        out.println(
"</tr>");
        out.println(
"<tr>");
        out.println(
"<td>Your email: </td>");
        out.println(
"<td><input type=\"text\" name=\"email\"></td>");
        out.println(
"</tr>");
          out.println(
"</table>");
        out.println(
"<br>");
          out.println(
"<input type=\"submit\" name=\"Submit\" value=\"submit\">");
        out.println(
"</form>");
        out.println(
"</body></html>");
    }

    
    
public void doPost(HttpServletRequest request, HttpServletResponse response)
        
throws ServletException,IOException{
        Enumeration paramNames 
= request.getParameterNames();
        String parName;
        
boolean emptyEnum = false;
        
if(!paramNames.hasMoreElements())
            emptyEnum 
= true;
            
        response.setContentType(
"text/html");
        PrintWriter out 
= response.getWriter();
        
        out.println(
"<html><head>");
        out.println(
"<title>Submitted Parameters</title></head><body>");
        
if(emptyEnum){
            out.println(
"<h2>Sorry, the request does net contain any parameters</h2>");
        }
else{
            out.println(
"<h2>Here are the submitted parameter values</h2>");
        }

        
while(paramNames.hasMoreElements()){
            parName 
= (String)paramNames.nextElement();
            out.println(
"<strong>" + parName + "</strong> :" + request.getParameter(parName));
            out.println(
"<br>");
        }

        out.println(
"</body></html>");
    }

}
posted on 2005-11-11 23:13 方佳玮 阅读(1314) 评论(0)  编辑  收藏 所属分类: Servlet

只有注册用户登录后才能发表评论。


网站导航: