目录
9.1 Servlet基础
Server + let 服务器端小应用程序
Servlet 是一个Java程序,是驻留在服务器端,运行以处理客户端请求并作出响应的程序。
功能是动态的生成网页,接受用户发送的请求和数据,然后根据这些请求和数据访问数据库等其他应用程序,
9.1.1 基础
必须重写父类方法
在编写后台代码时要停止汤姆猫
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doGet执行了");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost执行了");
}
}
9.1.2 Servlet的配置
如果前端想要访问java web应用中的后台Servlet程序,必须先配置Servlet的映射名称。
需要在web.xml
文件中添加配置信息
有两种方式可以配置Servlet:
- 使用MyEclipse向导
Servlet向导自动生成web.xml 中的映射信息,还会在Servlet类中生成doGet()和doPost()方法. - 手动配置
<servlet>
<servlet-name>HelloServlet</servlet-name>
<!-- 类的完全限定名 -->
<servlet-class>com.servlet.HelloServlet</servlet-class>
<!-- 请求路径名 这里验证路径是否正确的方法是:可以按住Ctrl然后左键路径,看能不能跳转到正确的路径-->
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
<!-- /随便一个名字 -->
</servlet-mapping>
通过表单验证post方式的请求:
新建 form.jsp
如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'form.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="hello">发送Servlet请求</a><br>
<form action="hello" method="post">
请输入信息:<input type="text" name="info">
<input type="submit" value="发送Servlet请求">
</form>
</body>
</html>
HelloServlet.java
如下:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//解决中文乱码问题,只对post有效,get需要要在tomcat的文件中修改
String info = request.getParameter("info");
System.out.println("收到客户端的请求数据:"+info);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>S</title>");
out.println("</head>");
out.println("<body>");
out.println(info+ "Hello client!!");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
9.1.3 乱码解决
客户端和服务器之间默认都是使用Latin1英文字符集编码,不支持中文
- 客户端到服务器的乱码:
get
在conf/server.xml
中添加配置信息:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
post
request.setCharacterEncoding("utf-8");
- 服务器到客户端的乱码:
response.setContentType("text/html;charset=UTF-8");
9.2 进阶
9.2.1 Servlet的生命周期
Servlet的生命周期由Servlet容器(如 Tomcat)控制
9.2.2 get和post
Get
- 是表单省略method时,默认的请求方式
- get方式请求的数据是明文显示在地址栏上的,不适合发送敏感数据
/login?username=admin&password=123…
/请求映射名?参数名1=参数值1&参数名2=参数值2&…
Post
- 在表单中使用最多的方式. 必须写上method=”post”
- post递交的含义,信息不是明文显示在地址栏上的.通过后台传递的较于get更安全的方式.
- post方式url地址栏字符长度不受限制
9.2.3 页面的跳转
分为两种页面跳转
- 服务器跳转
|- 请求转发 forward
步骤:
(1)获得请求转发器对象
(2)使用该对象进行跳转forward()
新建result.jsp
:
<body>
显示结果:<br>
两数的乘积为:result
</body>
//跳转页面
RequestDispatcher dispatcher = req.getRequestDispatcher("/result.jsp");
//利用转发器进行跳转
dispatcher.forward(req, resp);
整合成一句代码
req.getRequestDispatcher("/result.jsp").forward(req, resp);
服务器跳转地址不变
- 客户端跳转
|-请求重定向
resp.sendRedirect("result.jsp");
地址栏发生变化
服务器跳转和客户端跳转的区别
- 服务器跳转地址栏不变,依然后为请求的servlet-url信息
而客户端地址栏改变为页面的地址 - 服务器跳转可以在页面中访问请求作用域中的属性值
而客户端跳转不能访问请求作用域中的属性. - 服务器跳转只能在当前web应用中进行转发
而客户端跳转可以是任意URL信息.
9.2.4 Servlet作用域
服务器内存空间
- 请求作用域 request Scope
仅在客户端的一次请求过程中中有效 - 会话作用域 session scope
多次的请求与响应过程中有效【一次客户端的连接过程中有效】 - Servlet全局作用域
多个会话共享的数据【服务器的一次启动有效】