用jsp+Servlet实现简单的提交表单并显示的注册

目录结构:

用jsp+Servlet实现简单的提交表单并显示的注册_第1张图片

步骤:

1、注册表单页面register.jsp

2、处理注册表单信息并实现显示的Servlet:HomeWork.java

3、显示信息的页面:output.jsp

4、修改web.xml,配置servlet信息


register.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

  
    注册页面
  
  

注册页面填写



姓名

性别

出生年月

民族

个人介绍

    

output.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

  
    显示页面
  
  
  <% request.setCharacterEncoding("UTF-8");%> //防止乱码
  

用户注册信息如下:

<%=request.getAttribute("outputMessage") %>

HomeWork.java:

package servlets;

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

public class HomeWork extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8 ");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String yourname=request.getParameter("yourname");
	    String sex=request.getParameter("sex");
	    String userdate=request.getParameter("userdate");
	    String minzu=request.getParameter("minzu");
	    String introduction=request.getParameter("introduction");
		out.println("您的姓名是:"+yourname+"
"); out.println("您的性别是:"+sex+"
"); out.println("您的出生日期是:"+userdate+"
"); out.println("您的民族是:"+minzu+"
"); out.println("您的个人介绍是:"+introduction+"
"); out.flush(); out.close(); request.setAttribute("outputMessage", out); request.getRequestDispatcher("/output.jsp").forward(request,response); } }

修改web.xml:



  
    This is the description of my J2EE component
    This is the display name of my J2EE component
    HomeWork  //修改成你的servlet名
    servlets.HomeWork //要修改的地方
  
  
    HomeWork //要修改的地方
    /HomeWork //要修改的地方
  


你可能感兴趣的:(myeclipse)