Servlet的读取和显示功能

学后总结,仅供参考。

第一步:创建regXXX.html页面,使用<form>标签及相关控件包含单行文本框多行文本区单选按钮复选框下拉列表等

第二步:创建DealRegXXX.java,重写doPost()方法,在其中读取用户输入的所有信息,在读取信息之前,先进行编码转换:

request.setCharacterEncoding("GBK");

读取单值参数如用户姓名时,使用如下语句:

String username=request.getParameter("username");

读取多值参数如擅长技术时,使用如下语句:

String[] adept=request.getParameterValues("adept");

获取当前会话对象,使用如下语句:

HttpSession session=request.getSession();

向会话有效范围内存入信息,使用如下语句:

session.setAttribute("username", username);

跳转到另一个Servlet,使用如下语句:

response.sendRedirect("Result");

第三步:创建ResultXXX.java,重写doPost()方法,从会话有效范围内获取用户信息,并输出。

设置响应的contentType时,使用如下语句:

response.setContentType("text/html;charset=GBK");

从会话有效范围内获取信息,使用如下语句:

HttpSession session=request.getSession();

String username=(String)session.getAttribute("username");

获取当前的输出流对象时,使用如下语句:

PrintWriter out=response.getWriter();

向页面输出信息时,使用如下语句:

out.println("username");

第四步:调试、运行程序。检查显示结果是否正确。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


信息统计表
姓名:
年龄:
学号:
性别:
爱好: 读书
音乐
旅游
登山
上网
游泳
学历:
自我介绍:


package Servlet;

import java.io.IOException;

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 javax.servlet.http.HttpSession;

/**
 * Servlet implementation class DealRegTest
 */
@WebServlet("/DealReg0805")
public class DealReg0805 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DealReg0805() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String name1 = request.getParameter("name");
		HttpSession session = request.getSession();//创建一个session对象
		//将登陆的用户名存到session里面,向会话有效范围内存入信息
		session.setAttribute("name",name1);
		//读取单值参数
		String age = request.getParameter("age");
		session.setAttribute("age",age);
		String number = request.getParameter("number");
		session.setAttribute("number",number);
		String sex = request.getParameter("sex");
		session.setAttribute("sex",sex);
		//读取多值参数例如数组等
		String []hobby = request.getParameterValues("hobby");
		session.setAttribute("hobby",hobby);
		String degree = request.getParameter("degree");
		session.setAttribute("degree", degree);
		String comment = request.getParameter("comment");
		session.setAttribute("comment", comment);
		//跳转到resultTest
		response.sendRedirect("Result0805");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

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 javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.Out;

/**
 * Servlet implementation class ResultTest
 */
@WebServlet("/Result0805")
public class Result0805 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Result0805() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html;charset=UTF-8");
		//创建一个session对象,获取当前会话对象
		HttpSession session = request.getSession();
		//从会话有效范围内获取信息
		String name = (String)session.getAttribute("name");
		String age = (String)session.getAttribute("age");
		String number = (String)session.getAttribute("number");
		String sex = (String)session.getAttribute("sex");
		String []hobby = (String [])session.getAttribute("hobby");
		String degree = (String)session.getAttribute("degree");
		String comment = (String)session.getAttribute("comment");
		//获取当前的输出流对象
		PrintWriter out = response.getWriter();
		out.print("信息表
" +"" +"" +"" +""); out.print("" +"
信息统计表
姓名:"+name+"
年龄:"+age+"
学号:"+number+"
性别:"+sex+"
爱好:"); for(int i = 0;i
学历:"+degree+"
自我介绍:"+comment+"
"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }

你可能感兴趣的:(jsp)