Servlet是一个需要实现特定接口的实现类,他能接受http请求,只需要在web.xml进行一定的配置(2.5的做法)
public class HelloSetvlet implements Servlet {
public HelloSetvlet() {
System.out.println("你好!!");
}
@Override
public void destroy() {
System.out.println("销毁方法");
}
// 没用
@Override
public ServletConfig getServletConfig() {
return null;
}
// 没用
@Override
public String getServletInfo() {
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
System.out.println("初始化方法");
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("servlet方法");
}
}
web.xml
<!-- 注册一个servlet到tomcat -->
<servlet>
<!-- 注册的名字 -->
<servlet-name>hello</servlet-name>
<!-- 要注册的servlet权限定名 -->
<servlet-class>com.guodong.servlet.HelloSetvlet</servlet-class>
</servlet>
<!-- Servlet映射 -->
<servlet-mapping>
<!-- 注册的名字 -->
<servlet-name>hello</servlet-name>
<!-- hellod对应的servlet的映射路径 -->
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
生命周期
构造器:当访问Servlet时,会被调用且就被调用一次
init():在调用构造器之后立即调用也且调用一次
service():受理请求的,可以多次调用
destory():是销毁方法,当web应用在服务器上卸载时调用
load-on-startup
可以指定Servlet被创建的时机
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.neusoft.javaweb.HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
在加载完Servlet后直接就创建了Servlet实例,并进行了初始化,在此期间我们并没有提出请求。
如果配置了多个Servlet, load-on-startup的值越小越先启动(包括0,负数不会被启动,还是需要发出请求才创建实例并初始化);
Servlet映射细节
在Servlet映射到的URL中也可以使用 * 通配符,但是只能有两种固定的格式:
一种格式是“* .扩展名”
<servlet-mapping>
<servlet-name>AnyName</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
另一种格式是以正斜杠(/)开头并以“/ * ”结尾。
<servlet-mapping>
<servlet-name>AnyName</servlet-name>
<url-pattern>/action/*</url-pattern>
</servlet-mapping>
注意:既带 / 又带 * 的,又带扩展名的,是不合法的(例如:/*.do)
Servlet3.0
采取注解的方式
@WebServlet("/hello")
public class HelloServet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理get请求
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理POST请求
}
}
如何获取请求参数
index.jsp
<form action="hello" method="get">
<p><input type="text" placeholder="用户名" name="username"></p>
<p><input type="password" placeholder="密码" name="password"></p>
<p>
<input type="radio" name="gender" value="1"> 男
<input type="radio" name="gender" value="2"> 女
</p>
<p>
<input type="checkbox" name="hobby" value="篮球"> 篮球
<input type="checkbox" name="hobby" value="足球"> 足球
<input type="checkbox" name="hobby" value="排球"> 排球
<input type="checkbox" name="hobby" value="混球"> 混球
</p>
<button>提交</button>
</form>
HelloServlet1.java
@WebServlet("/hello")
public class HelloServet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求参数
String username = request.getParameter("username");
String password = request.getParameter("password");
String gender = request.getParameter("gender");
// String hobby = request.getParameter("hobby");
// 获取复选框请求参数
String hobby[] = request.getParameterValues("hobby");
System.out.println(username+","+password+","+gender+",");
for(String str : hobby) {
System.out.println("^^"+str);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
初始化参数
2.5配置
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.neuedu.servlet.HelloServlet</servlet-class>
<!-- 初始化参数username = tom -->
<init-param>
<param-name>username1</param-name>
<param-value>tom1</param-value>
</init-param>
<init-param>
<param-name>username2</param-name>
<param-value>tom2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
3.0配置
@WebServlet(
urlPatterns = "/loginServlet",
initParams = {
@WebInitParam(name="password",value="666")
}
)
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
System.out.println("^^"+id);
String path1 = getServletContext().getContextPath();
String path2 = request.getContextPath();
System.out.println(path1+","+path2);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 请求时支持中文
request.setCharacterEncoding("utf-8");
// 响应时支持中文
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
// 1.获取请求参数 - 表单提交过来的user和pass
String user = request.getParameter("user");
System.out.println(user);
String pass = request.getParameter("pass");
// 2.获取全局初始化参数 username
String username = getServletContext().getInitParameter("username");
// 3.获取当前Servlet初始化参数 password
String password = getServletConfig().getInitParameter("password");
// 4.对比,如果user和username相同,且pass和password相同,就响应Hello:xxx,否则响应Sorry:数据有误
if(user.equals(username) && pass.equals(password)) {
response.getWriter().print("Hello:"+username);
}else {
response.getWriter().print("Sorry:数据有误");
}
}
}
全局初始化参数web.xml
<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">
<context-param>
<param-name>username</param-name>
<param-value>mike</param-value>
</context-param>
</web-app>
POST和GET请求
Get 请求是将请求参数拼接在请求URL路径的后面,以?隔开,以key=value的方式传递 ,多个参数xxxx?key1=value1&key2=value2...
Get 是不安全的,因为在传输过程,数据被放在请求的URL 中;Post 的 所有操作对用户来说都是不可见的;
Get 传输的数据量小,这主要是因为受URL 长度限制(1K);而Post 可以传输大量的数据,所以在上传文件只能使用Post;
网友评论