HttpServlet类的请求与响应

来源:互联网 发布:ubuntu下安装sqlyog 编辑:程序博客网 时间:2024/05/22 14:18

请求与响应一般都是表单与服务器发生交互的,可以使用接口httpServletRequest,httpServletResponse实现请求与交互,下面来看一段请求与交互的代码

   首先新建一个javaweb项目,在这个项目下面新建一个servlet对象。然后新建Info.html文件,再配置web.xml文件


代码如下:1.java文件

package hzy;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 studentInfo */@WebServlet(description = "this is student message", urlPatterns = { "/studentInfo" })public class studentInfo extends HttpServlet {private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public studentInfo() {        super();        // TODO Auto-generated constructor stub    }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("gb2312");response.setContentType("text/html;charset=gb2312");PrintWriter out=response.getWriter();out.println("<html>");out.println("<head>");out.println("<title>基本信息</title>");out.println("</head>");out.println("<body>");out.println("<h2>学生的基本信息</h2>");out.println("<h3>姓名:"+request.getParameter("sname")+"</h3>");out.println("<h3>学号:"+request.getParameter("snumber")+"</h3>");out.println("<h3>班级:"+request.getParameter("sclass")+"</h3>");out.println("</body>");out.println("</html>");}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}}

2.html文件

<html>  <head>    <title></title>  </head>    <body>    <form action="studentInfo" name="myform" method="post">                         姓名:<input type="text" name="sname"><br>    学号:<input type="text" name="snumber"><br>   班级:<input type="text" name="sclass"><br>             <input type="submit" value="提交">    </form>  </body></html>


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>251Servlet</display-name>  <servlet>  <servlet-name>studentInfo</servlet-name>  <servlet-class>hzy.studentInfo</servlet-class>  </servlet>    <servlet-mapping>  <servlet-name>studentInfo</servlet-name>  <url-pattern>/studentInfo</url-pattern>  </servlet-mapping>  <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></web-app>
运行效果如下:
<img src="http://img.blog.csdn.net/20160604120340526?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />



0 0
原创粉丝点击