java web
1.环境搭建
环境那个建
2.了解MVC模型
model–view–controller
3.搭建登陆页面
HTML代码:
<body background="image/bg.jpg">
<p align="center" class="box">
<form id="form"method="post" action="servlet/LoginServlet">
userName:<input type="text" value="${userName }" name="userName" id="userName"/><p/>
password:<input type="password" value="${password }" name="password" id="password"/><br/>
<p/>
</form>
<a class="btn_style" id="btn_login"href="javascript:login()" >登陆</a>
<a class="btn_style" id="btn_reset"href="JavaScript:reset()">重置</a>
<br/>
<font id="ERROR"color="red">${error }</font>
</p>
</body>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
Javascript代码:
<script type="text/javascript">
function reset(){
document.getelementbyid("userName").value = "";
document.getElementById("password").value = "";
document.getElementById("error").innerHTML = "";
}
function login(){
document.getElementById('form').submit();
}
</script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
CSS代码:
<style type="text/css">
.box{
width: 400px;
height: 300px;
margin:200 auto;
display:block;
position: relative;
}
.btn_style{
width: 100px;
height: 30px;
display: block;
position:absolute;
text-decoration:none;
text-align:center;
line-height:30px;
color: #fff;
background-color: #058;
}
#btn_reset:HOVER {
background-color: #047;
}
#btn_login:hover{
background-color: #047;
}
#btn_reset{
right:50px;
}
#btn_login{
margin-left: 50px;
}
</style>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
4.数据库设计
5.工具类
连接数据库,导入jar包:
public class DbUtil {
private String dbUrl = "jdbc:mysql://localhost:3306/db_manage";
private String dbUserName = "root";
private String dbPassword = "";
private String jdbcName = "com.mysql.jdbc.Driver";
/*
* 获取连接
*/
public Connection getCon() throws ClassnotfoundException, SQLException{
class.forname(jdbcName);
Connection con = DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
return con;
}
/*
* 关闭连接
*/
public void closeCon(Connection con) throws SQLException{
if(null!=con){
con.close();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
字符串工具:
public class StringUtil {
public static boolean isempty(String str){
if("".equals(str)|| str==null){
return true;
}else{
return false;
}
}
public static boolean isNotEmpty(String str){
if(!"".equals(str)&&str!=null){
return true;
}else{
return false;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
6.写LoginServlet类
主要是处理:
public void doGet(HttpServletrequest request, HttpServletresponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getparameter("userName");
String password = request.getParameter("password");
System.out.println(userName+"--"+password);
if(StringUtil.isEmpty(password)||StringUtil.isEmpty(userName)){
//session
HttpSession session=request.getSession();
session.setattribute("error", "不能为空");
response.sendredirect(request.getcontextpath()+"/index.jsp");
System.out.println("kong");
return ;
}
User user = new User(userName, password);
try {
Connection con = dbUtil.getCon();
User curr_user = userDao.login(con, user);
if(null==curr_user){
HttpSession session=request.getSession();
session.setAttribute("error", "错误");
session.setAttribute("userName", userName);
session.setAttribute("password", password);
response.sendRedirect(request.getContextPath()+"/index.jsp");
System.out.println("error");
}else{
//
HttpSession session=request.getSession();
session.setAttribute("currentUser", "登陆成功");
//ת
response.sendRedirect(request.getContextPath()+"/main.jsp");
System.out.println("yes");
}
} catch (ClassNotFoundException e) {
e.printstacktrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
7.在web.xml中配置servlet
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>jimo.web.LoginServlet</servlet-class>
</servlet>
<servlet-mAPPing>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
8.验证
main.jsp页面很简单:
<font color="red">${currentUser }</font>
- 1
为空时:
错误时:
成功时:
9.bug修复
Bug:我们不登录,直接输入http://localhost:8080/Test/main.jsp
也能进入主页面
修复Bug:
在main.jsp前面加入判断:
<%
if(null==session.getattribute("currentUser")){
System.out.print("ddd");
response.sendRedirect("index.jsp");
return;
}else{
System.out.print("yyy");
}
%>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
10.总结
1.最好采用post方式提交,更加安全
2.登陆为空验证:
前端验证:不安全
后台验证:推荐
3.涉及服务器端跳转到客户端
4.客户端通过EL表达式${error}获取传过来的信息
5.为了用户体验好,信息错误也要传过去
相关阅读
淘宝达人也是店铺常用的推广方式之一,和淘宝客差不多需要支付佣金。达人是淘宝网上对相关领域有专业认识的并乐于购物乐于分享的一
<!doctype html> <html> <head> <meta charset="utf-8"> <title>login</title> <style type="text/css"> *{ margin: 0; paddin
试着编写了一下战网的登录页面,刚学会css和html5 暴雷游戏通行证登录
windows利用默认帐号自动登录 1、控制面板-用户帐户-更改用户的登陆或注销的方式,把使用欢迎屏幕前打勾 。 2、建立了一个新的非受
创建index.jsp文件,在其中添加用于收集用户登录信息的表单及表单元素:<%@ page language="java" contentType="text/html; charset=