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
- HttpServlet类的请求与响应
- HttpServlet容器响应Web客户请求流程
- HttpServlet容器响应Web客户请求
- HttpServlet容器响应Web客户请求
- Ajax 的请求与响应
- HTTP的请求与响应
- J2EE中,HttpServlet容器响应Web客户端请求流程
- http协议的请求与响应
- jquery的ajax请求servlet与响应
- 请求与响应的大体流程
- 浏览器与IIS的请求处理响应
- Android的UDP请求与响应
- 完整的web请求与响应
- HTTP协议的请求与响应
- 一个完整的请求与响应过程
- HTTP的请求报文与响应报文
- 一次完整的 HTTP 请求与响应
- 完整的HTTP请求与响应(图解)
- cocoa pods 错误分析
- C陷阱与缺陷中有关scanf的一道题
- NopCommerce是什么
- php日记(二)---yii
- http请求过程
- HttpServlet类的请求与响应
- 运动目标检测--背景减法
- java读取word中的表格并存入到mysql数据库中实例
- 解决虚拟机中安装的mac os系统无法识别ios设备的问题
- 高低温试验箱的相关知识和作用分析
- Web前端初入
- poj 1236
- java——认识队列和运算机制
- 第十五周项目 洗牌