Servlet之输入交互-------------利用Servlet表单输出个人信息

1.编写主页index.jsp

为了统一编码,这里用utf-8编码

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

    pageEncoding="utf-8"%>




个人信息的输入




请输入个人信息





输入姓名:

输入年龄:

输入学号:









2.编写Servlet工程的PersonInformation类,作为初学者,java包的定义一切从简


package Servlet;
import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class PersonInformation
 */
@WebServlet("/PersonInformation")
public class PersonInformation extends HttpServlet {
private static final long serialVersionUID = 1L;


    /**
     * Default constructor. 
     */
    public PersonInformation() {
        // 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());
}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html; charset=utf-8");
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String age=request.getParameter("age");
String stu_number=request.getParameter("stu_number");
PrintWriter out=response.getWriter();
out.println("个人信息");
out.println("

这是你的个人信息


");
out.println("姓名:"+name+"
");
out.println("年龄:"+age+"
");
out.println("学号:"+stu_number+"
");
out.println("");
out.close();
}


}


3.web.xml的配置,这里你得注意你的类与包之间的关系,以免搞死tomcat容器

    

  testForm2
 
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
 

 
  PersonInformation
  Servlet.PersonInformation
 

 
  PersonInformation
  /Servlet/PersonInformation
 


4.运行结果

    Servlet之输入交互-------------利用Servlet表单输出个人信息_第1张图片Servlet之输入交互-------------利用Servlet表单输出个人信息_第2张图片

你可能感兴趣的:(java,j',v)