01_Servlet概述
来源:互联网 发布:登陆淘宝账户 编辑:程序博客网 时间:2024/06/10 12:46
Servlet是什么
就是一个Java类
服务器端小程序
处理用户请求
Servlet的实现
实现Servlet接口
继承GenericServlet类
继承HttpServlet类
编写Servlet
1.新建web项目(helloservlet)
2.编写HelloServlet类
package com.zhushen.Servlet;import java.io.IOException;import javax.servlet.Servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class HelloServlet implements Servlet { public void destroy() { } public ServletConfig getServletConfig() { return null; } public String getServletInfo() { return null; } public void init(ServletConfig config) throws ServletException { } //处理请求 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { res.getOutputStream().print("Hello Servlet!"); } public HelloServlet() { }}
3.在web.xml中部署Servlet
<!-- Servlet的配置 包括两个部分 Servlet Servlet-Mapping --> <servlet> <!-- Servlet-name可以自定义,但是要唯一 --> <servlet-name>helloservlet</servlet-name> <!-- Servlet-class Servlet的完全限定名:包名.类名 检查是否写错的办法:按住ctrl+鼠标看是否能够连接 --> <servlet-class>com.zhushen.Servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <!-- 和Servlet标签中的Servlet-name要一致 --> <servlet-name>helloservlet</servlet-name> <!-- 也可以叫做Servletname:这个名称是访问名称:请求路径 --> <url-pattern>/hello</url-pattern> </servlet-mapping>
4.访问:localhost:8080/helloservlet/hello(项目名-访问名)
servlet小案例
- web.xml
<servlet> <servlet-name>helloservlet</servlet-name> <servlet-class>com.zhushen.Servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloservlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
- User类
package com.zhushen.vo;public class User { private String name; private int age; private String sex; private String phone; public User() { } public User(String name, int age, String sex, String phone) { super(); this.name = name; this.age = age; this.sex = sex; this.phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; }}
- helloservlet
package com.zhushen.Servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import javax.servlet.Servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import com.zhushen.vo.User;public class HelloServlet implements Servlet { public void destroy() { } public ServletConfig getServletConfig() { return null; } public String getServletInfo() { return null; } public void init(ServletConfig config) throws ServletException { } //处理请求 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { List<User> list =new ArrayList<User>(); list.add(new User("张三丰",45,"男","17801020294")); list.add(new User("小芳",22,"女","17801020294")); list.add(new User("小红",20,"女","17801020294")); list.add(new User("小明",25,"男","17801020294")); //响应转码,默认ISO8859-1 res.setCharacterEncoding("utf-8");//必须放在前面 PrintWriter os =res.getWriter(); res.setContentType("text/html;charset=utf-8"); os.print("<html>"); os.print("<head>"); os.print("<title>"); os.print("数据系统"); os.print("</title>"); os.print("</head>"); os.print("<body>"); os.print("<table align='center' width='80%'>"); os.print("<tr>"); os.print("<td>"); os.print("姓名"); os.print("</td>"); os.print("<td>"); os.print("年龄"); os.print("</td>"); os.print("<td>"); os.print("性别"); os.print("</td>"); os.print("<td>"); os.print("电话"); os.print("</td>"); os.print("</tr>"); for(int i=0;i<list.size();i++){ os.print("<tr>"); os.print("<td>"); os.print(list.get(i).getName()); os.print("</td>"); os.print("<td>"); os.print(list.get(i).getAge()); os.print("</td>"); os.print("<td>"); os.print(list.get(i).getSex()); os.print("</td>"); os.print("<td>"); os.print(list.get(i).getPhone()); os.print("</td>"); os.print("</tr>"); } os.print("</table>"); os.print("</body>"); os.print("</html>"); } public HelloServlet() { }}
阅读全文
0 0
- 01_Servlet概述
- 01_Servlet 教程
- 第一章_servlet
- 【Servlet3.0新特性】第01节_Servlet注解及异步支持
- Servlet学习笔记_Servlet
- 03_Servlet编程
- 黑马程序员_Servlet
- web.xml(5)_servlet
- 黑马程序员_Servlet生命周期
- 02_Servlet 简介
- 03_Servlet 环境设置
- 04_Servlet 生命周期
- 05_Servlet 实例
- 06_Servlet 表单数据
- 10_Servlet 编写过滤器
- 11_Servlet 异常处理
- 12_Servlet Cookies 处理
- 13_Servlet Session 跟踪
- Mac挂载远程目录到本地
- C# 窗体间传值方法大汇总(转)
- morphia @Reference注解
- 【算法】插入排序(从小到大) 排序范围(0~n-1)n为数组元素个数
- JAVA之路——前端与后端的职业选择
- 01_Servlet概述
- 欢迎使用CSDN-markdown编辑器
- storyboard autolayout 约束距底部空间跳动问题
- 欢迎使用CSDN-markdown编辑器
- CSS之中间固定两边自适应宽度
- opendaylight实现无线路由连接 -2
- 1.数组初始化
- iOS按钮设置图片在上文字在下
- linux下编译安装libcurl(附使用示例)