1.Servlet简介
1.简介:Servlet和Jsp共同的称为Java Web层的两大组件。每次运行都是启动的一个线程,比原先的CGI更优越,节省内存,增加并发度。
2.功能:
-
生成网页
-
处理表单
-
与数据库进行交互
-
调用JavaBean
3.优点:
-
移植性
-
功能强大
-
安全性
-
集成性好
2.登录案例
开发环境:Eclipse2017
1.案例实现
第一步:创建一个web项目ch02-01.1
-
src:放servlet文件
-
WebContent:放静态页面
第二步:创建一个登录页面index
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>index</title>
<style>
* {
margin: 0;
padding: 0;
}
html {
height: 100%;
}
body {
height: 100%;
}
.container {
height: 100%;
background-image: linear-gradient(to right, #e5d7ff, #a6c1ee);
}
.login-wrapper {
background-color: #fff;
width: 358px;
height: 588px;
border-radius: 15px;
padding: 0 50px;
position: relative;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.header {
font-size: 38px;
font-weight: bold;
text-align: center;
line-height: 200px;
}
.input-item {
display: block;
width: 100%;
margin-bottom: 20px;
border: 0;
padding: 10px;
border-bottom: 1px solid rgb(128, 125, 125);
font-size: 15px;
outline: none;
}
.input-item:placeholder {
text-transform: uppercase;
}
#btn {
text-align: center;
padding: 10px;
width: 100%;
margin-top: 40px;
background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
color: #fff;
}
.msg {
text-align: center;
line-height: 88px;
}
a {
text-decoration-line: none;
color: #abc1ee;
}
</style>
</head>
<body>
<div class="container">
<div class="login-wrapper">
<div class="header">登录</div>
<div class="form-wrapper">
<form action="login" method="get">
<input type="text" name="username" placeholder="请输入用户名" class="input-item">
<input type="password" name="password" placeholder="请输入密码" class="input-item">
<input type="submit" value="提交" id="btn" style="border-style: none;"/>
</form>
</div>
<div class="msg">
没有账号,点我注册>
<a href="#">注册</a>
<div id="app">
<a>阅读隐私协议</a>
<input type="checkbox" />
</div>
</div>
</div>
</div>
</body>
</html>
第三步:编写servlet界面
配置servlet有两种方式一种是注解的方式,一种是配置文件的方式。
先讲xml配置的方式,再讲注解配置的方式。推荐使用注解。
创建servlet需要继承HttpServlet接口。
第一种创建方式:
-
使用@WebServlet("/login")注解
package myservlet;
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;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 将request的字符编码设置为utf-8
request.setCharacterEncoding("utf-8");
// 设置响应的编码为utf-8
response.setCharacterEncoding("utf-8");
// 获取请求的用户名和密码,验证正确性
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+"---"+password);
// 获取输出流对象
PrintWriter out=response.getWriter();
// 设置html页面的字符编码为utf-8
response.setHeader("content-type", "text/html;charset=utf-8");
if(username.equals("root")&&password.equals("123456")) {
// 登录成功!
out.append("欢迎"+username+"登录成功!");
}
else {
// 登录失败
out.append("对不起,请重新登录!");
}
}
}
第二种创建方式:使用xml配置。
-
把注解删除,其余的地方不变,在web.xml中加入如下的语句
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>myservlet.LoginServlet</servlet-name>
<servlet>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-mapping>/login</servlet-mapping>
<servlet>
第四步:启动项目,运行html页面
-
假设账号为root,密码为123456才是正确的信息
index界面
输入账号和密码:
登录:
失败: