Servlet之输入交互-------------利用Servlet文本框

1.创建首页index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Front Page of Ex36</title>
</head>
<body>
<form method="post" action="Servlet/Ex36">
<p align="left">
<font size="5">
<b>Front page of Ex36</b>
</font>
</p>
<p> </p>
<p align="left">
输入数据:<br>
<textarea  name="data"   rows="3" cols="30"></textarea><br>
<input type="submit" value="发送">
<input type="reset"  value="取消">
</p>
</form>
 
</body>

</html>

2.编辑网站程序

    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 Ex36
 */
@WebServlet("/Ex36")
public class Ex36 extends HttpServlet {
private static final long serialVersionUID = 1L;


    /**
     * Default constructor. 
     */
    public Ex36() {
        // 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");
PrintWriter out=response.getWriter();
String val=request.getParameter("data");
out.println("<html><head><title></title></head><body>");
out.println("This is the sub page of Ex38<br>");
out.println("<br>");
out.println("Data:"+val+"<br>");
out.println("</body></html");
out.close();

}


}

3.xml的配置

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>testText</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <servlet>
    <servlet-name>Ex36</servlet-name>
    <servlet-class>Servlet.Ex36</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Ex36</servlet-name>
    <url-pattern>/Servlet/Ex36</url-pattern>
  </servlet-mapping>

</web-app>

运行结果

    Servlet之输入交互-------------利用Servlet文本框Servlet之输入交互-------------利用Servlet文本框