Servlet
-
Java类必须要符合一定的规范
-
a.必须继承 javax.servlet.http.HttpServlet
b.重写其中的doGet()或者doPost()方法
- doGet():接受并处理所有get提交方式的请求
- doPost():接受并处理所有post提交方式的请求
-
Servlet要想使用,必须配置
-
Servlet2.5:web.xml
Servlet3.0:@Servlet
Servlet使用层面
-
在Eclipse中的src创建一个Servlet,然后重写doGet()、doPost()方法即可。
-
项目的根目录:WebContent 、src(所有的构建路径)
-
可借助eclipse快速生成Servlet(继承、重写、web.xml可以借助eclipse自动生成)----Servlet3.0
使用servlet2.5完成用户名密码登录,并实现简单的验证
代码
- index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<style>
.box{
position: absolute;/*生成绝对定位的元素,元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。(此处相对于body)*/
top: 50%;
left: 50%;
transform: translate(-50%,-50%);/* 实现块元素百分比下居中*/
width: 400px;
padding: 40px;/*设置四个内边距*/
background: rgba(0,0,0,0.8);/*函数的前三个变量为颜色(黑),后一个为透明度(1为不透明)*/
box-sizing: border-box;/*使实际所占宽高度 = 设置的高度(height)/ 设置的宽度(width)+ 外边距(margin)*/
box-shadow: 0 15px 25px rgba(0,0,0,.5);/*产生盒子阴影:x轴偏移,y轴偏移,模糊度,透明色*/
border-radius: 10px;/*使矩形四边圆滑*/
}
.box h2{
margin: 0 0 30px;/*上、左右、下*/
padding: 0;
color: #ffffff;/*设置字体颜色*/
text-align: center;/*文字居中*/
}
.box .inputBox{
position: relative;/*相对定位*/
}
.box .inputBox input{
width: 100%;
padding: 10px 0;/*上下、左右*/
font-size: 16px;/*字体大小(可用于调节输入框高)*/
color: #ffffff;/*输入框颜色*/
letter-spacing: 1px;/*设置对象中的文字之间的间隔*/
margin-bottom: 30px;/*设置下外距*/
border: none;/*定义无边框*/
border-bottom: 1px solid #ffffff;/*底边框:大小1PX 实线 颜色#ffffff */
outline: none;/*设置无外边框*/
background: transparent;/*背景设置为透明*/
}
.box .inputBox label{
position: absolute;
top: 0;
left: 0;
letter-spacing: 1px;
padding: 10px 0;
font-size: 16px;
color: #ffffff;
pointer-events:none;/*阻止区块被点击,使得点击穿透*/
transition: 1s;/*动画运行时间*/
}
/*产生对应操作时,label发生的样式变化*/
.box .inputBox input:focus~label,
.box .inputBox input:valid~label
{
top:-18px;
left: 0;
color: #03a9f4;
font-size: 12px;
}
.box input[type="submit"]{
background: transparent;
border: none;
outline: none;
color: #ffffff;
background: #03a9f4;
margin: 0px 80px;
padding: 10px 30px;
cursor: pointer;/*鼠标指针变成手的形状*/
border-radius: 15px;/*调节矩形弧度*/
}
</style>
<body>
<div class="box">
<h2>Usermanage</h2>
<!--创建供用户输入的 HTML 表单-->
<form action="checkServlet" method="post">
<div class="inputBox">
<input type="text" name="uname" required="">
<label>用户名</label>
</div>
<div class="inputBox">
<input type="password" name="upwd" required="">
<label>密码</label>
</div>
<div align="center">
<input type="submit" name="" value="登录">
</div>
</form>
</div>
</body>
</html>
- web.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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>UserManage1.0</display-name>
<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>
<servlet>
<servlet-name>checkServlet</servlet-name>
<servlet-class>com.zr.checkServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>checkServlet</servlet-name>
<url-pattern>/checkServlet</url-pattern>
</servlet-mapping>
</web-app>
- checkServlet.java
package com.zr;
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 checkServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO 自动生成的方法存根
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
//设置request和response的编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取表单属性值
String username=request.getParameter("uname");
String password=request.getParameter("upwd");
if(username.equals("钟燃")&&password.equals("123")) {
//输出注册结果
PrintWriter out=response.getWriter();//获取PrintWriter对象
out.print("<h1 aling='center'>");
out.print(username+"注册成功!");
out.print("</h1>");
out.flush();
out.close();
}else {
PrintWriter out=response.getWriter();//获取PrintWriter对象
out.print("<h1 aling='center'>");
out.print("用户名密码错误!");
out.print("</h1>");
out.flush();
out.close();
}
}
}
演示
servlet生命周期
加载
初始化:init(),该方法会在servlet被加载并实例化以后执行
服务:service() -->doGet()、doPost();
销毁:destroy(),servlet被系统回收时执行
卸载
Servlet API
由两个软件包组成:对应于HTTP协议的软件包、对应于除了HTTP协议以外的其他软件包,即Servlet API可以适用于任何通信协议。