servlet 第二次课堂测试
1.创建一个登录界面的html
2.servlet获取到html界面提交的用户名密码
3.用户名密码一致不为空则能够登录,并且显示欢迎界面输出当前的用户名
4.如果密码错误则弹窗提示错误,返回登录界面
5.创建两个html的banner和address
6.分别用类去加载这两个html来实现banner+内容+address的界面
login.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset=UTF-8>
<title>
Insert titile here
</title>
</head>
<body>
<form action="LoginServlet1" method="post" >
用户名:<input type="text" name="username" /><br>
密 码:<input type="password" name="password" /><br>
<input type="submit" value="登录" />
</form>
</div>
</body>
</html>
welcome.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset=UTF-8>
<title>
Insert titile here
</title>
<body>
欢迎
</body>
</head>
</html>
banner.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
ul {
list-style-type: none;
}
.top {
position: fixed;
left: 0;
right: 0;
top: 0;
z-index: 999;
height: 50px;
background-color: rgba(0, 0, 0, 0.6);
}
.menu {
width: 1000px;
margin: 0 auto;
overflow: hidden;
}
.logo {
float: left;
margin-top: 9px;
}
.logo a {
display: inline-block;
width: 32px;
height: 32px;
background-image: url("http://temp.im/32x32/4CD964/fff");
background-size: 32px 32px;
}
.nav {
float: right;
margin-top: 10px;
font-size: 0;
}
.nav li {
display: inline-block;
margin-left: 40px;
}
.nav a {
display: inline-block;
height: 30px;
line-height: 30px;
font-size: 12px;
color: #fff;
text-decoration: none;
}
.nav a:hover {
color: #83c3f3;
}
</style>
</head>
<body>
<div class="top">
<div class="menu">
<h1 class="logo"><a href="http://www.dowebok.com/"></a></h1>
<ul class="nav">
<li>
<a href="https://www.baidu.com:">首页</a>
</li>
<li>
<a href="https://www.baidu.com:">产品</a>
</li>
<li>
<a href="https://www.baidu.com:">技术</a>
</li>
<li>
<a href="https://www.baidu.com:">渠道</a>
</li>
<li>
<a href="https://www.baidu.com:">论坛</a>
</li>
<li>
<a href="https://www.baidu.com:">关于我们</a>
</li>
</ul>
</div>
</div>
</br>
</br>
</br>
<div>
</body>
</html>
address.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<style type="text/css">
footer {
background-color: #457B9D;
position: absolute;
bottom: 0; /* 4. 设置页脚position为absolute之后,再将其bottom值设为0,即可使其处于页面的底部 */
width: 100%;
text-align: center;
}
</style>
<body>
<footer>
<p>我是页脚</p>
</footer>
</body>
</html>
LoginServlet类:
package ch04;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String username = (String) request.getAttribute("usernamedata"); //接受另一个类传递过来的数据
String password = (String) request.getAttribute("passworddata");
RequestDispatcher toubu = request.getRequestDispatcher("banner.html"); //文件包含banner和address的html文件
RequestDispatcher weibu = request.getRequestDispatcher("address.html");
if((username).equals(password) && username!=null && password != null){ //判断用户密码非空和一样
toubu.include(request, response); //输出包含的banner.html文件
out.println("<p>success,username is:"+username+"</p>");
weibu.include(request, response); //输出包含的address.html文件
}else{
out.print("<script> alert('password error');window.location.href='login.html';</script>");
//用户输入密码用户不一致会弹窗错误并跳转到登录界面
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doGet(request,response);
}
}
LoginServlet1类:
package ch04;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet1 extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
request.setAttribute("usernamedata",username); //获取用户提交的用户名和密码
request.setAttribute("passworddata",password);
RequestDispatcher dispatcher = request.getRequestDispatcher("LoginServlet"); //转发给另一个类
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doGet(request,response);
}
}