javaweb-实现不同⽤户登录后跳转到不同界⾯今天下午,实现了公⽂流转系统的⼀⼩部分——登录界⾯验证不同⽤户跳转到不同界⾯
Dao
package com.office.Dao;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.office.DBUtil.DBUtil;
public class Dao {
public String dopost(String username,String password) {
String i="-1";
String sql="select * from user where username = '"+username+"'";
Connection conn = Conn();
Statement state = null;
ResultSet rs = null;
try {
state = ateStatement();
rs = uteQuery(sql);
()){
String password1 = rs.getString("password");
if(password.equals(password1)) {
String("id");
}
break;
}
}catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs,state, conn);
}
return i;
}
}
Servlet
package com.office.Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.office.Dao.Dao;
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servlet() {
super();
}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String method = Parameter("method");
if ("dopost".equals(method)) {
dopost(req, resp);
}
}//end service
private void dopost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
String username = Parameter("username");
String password = Parameter("password");
Dao dao=new Dao();
String id=dao.dopost(username, password);
if(id.equals("-1")) {
req.setAttribute("message", "登录失败!");
else if(id.equals("0")) {
req.setAttribute("message", "登陆成功!");
}
else if(id.equals("1")) {
req.setAttribute("message", "登陆成功!");
}
}
}
User
package com.office.Entity;
public class User {
private String username;
private String password;
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
DBUtil
package com.office.DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
public static String db_url = "jdbc:mysql://localhost:3306/luizhuan"; public static String db_user = "root";
public static String db_pass = "wyp6666";
public static Connection getConn () {
Connection conn = null;
try {
Class.forName("sql.jdbc.Driver");
conn = Connection(db_url, db_user, db_pass); } catch (Exception e) {
e.printStackTrace();
}
return conn;
}//end getConn
public static void close (Statement state, Connection conn) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws SQLException {
Connection conn = getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql ="select * from user";
pstmt = conn.prepareStatement(sql);
rs = uteQuery();
()){
System.out.println("连接成功");
}else{
System.out.println("连接失败");
}
javascript登录注册界面}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<%
Object message = Attribute("message");
if (message != null && !"".equals(message)) {
%>
<script type="text/javascript">
alert("<%=Attribute("message")%>");
var Attribute("username");
</script>
<%
}
%>
<form action="Servlet?method=dopost" method="post">
<div>
⽤户名<input type="text" name="username" />
</div>
<div>
密码<input type="password" name="userpassword" />
</div>
<div>
<input type="submit" value="登录" />
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论