一、实验目的:
理解 Servlet 的工作原理,掌握 Servlet 的应用开发方法;掌握常见 Servlet ;
二、实验环境:
1.理解 Servlet 的应用。
2.掌握 Servlet 的编程技术。
3.熟练使用 Eclipse 开发 Servlet 应用
三、实验内容:
(一)用 Servlet 获取表单数据
1. 建立一个 Servlet 文件 accept.java 和一个 register.jsp 文件,实现用 Servlet 实现获取表单数据的功能。
Accept.java
- package com.yiibai;
-
- import java.io.IOException;
-
- import java.io.PrintWriter;
-
- import javax.servlet.ServletConfig;
-
- import javax.servlet.ServletException;
-
- import javax.servlet.annotation.WebServlet;
-
- import javax.servlet.http.HttpServlet;
-
- import javax.servlet.http.HttpServletRequest;
-
- import javax.servlet.http.HttpServletResponse;
-
- import java.io.*;
-
- import javax.servlet.*;
-
- import javax.servlet.http.*;
-
- /**
-
- * Servlet implementation class accept
-
- */
-
- @WebServlet("/accept")
-
- public class accept extends HttpServlet {
-
- public String codeToString(String str)
-
- {//处理中文字符串的函数
-
- String s=str;
-
- try
-
- {
-
- byte tempB[]=s.getBytes("ISO-8859-1");
-
- s=new String(tempB);
-
- return s;
-
- }
-
- catch(Exception e)
-
- {
-
- return s;
-
- }
-
- }
-
- public void init(ServletConfig config) throws ServletException
-
- {
-
- super.init(config);
-
- }
-
- public void doPost(HttpServletRequest request,HttpServletResponse response)
-
- throws ServletException,IOException
-
- {
-
- //设置mime
-
- response.setContentType("text/html;charset=utf-8");
-
- PrintWriter out=response.getWriter();
-
- out.println("<HTML><head><title>接收新用户注册title>head><BODY>");
-
- out.println("这是新用户注册所提交的数据:<br>");
-
- out.println("用户名是:"+codeToString(request.getParameter("username"))+"<br>");
-
- out.println("密码是:"+codeToString(request.getParameter("userpassword"))+"<br>");
-
- out.println("性别是:"+codeToString(request.getParameter("sex"))+"<br>");
-
- out.println("出生年月是:"+request.getParameter("year")+request.getParameter("month")+request.getParameter("day")+"<br>");
-
- out.println("电子邮箱是:"+request.getParameter("email")+"<br>");
-
- out.println("家庭住址是:"+codeToString(request.getParameter("address"))+"<br>");
-
- out.println("body> html>");
-
- }
-
-
-
- /**
-
- * @see HttpServlet#HttpServlet()
-
- */
-
- public accept() {
-
- super();
-
- // TODO Auto-generated constructor stub
-
- }
-
-
-
- /**
-
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
-
- */
-
- protected void doGet1(Htt