idea基于struct2实现登陆
时间: 2023-05-26 22:06:59 浏览: 118
一个基于struct2的登陆实现,大致思路如下:
1.前端页面设计:设计一个登陆页面,包括用户名、密码、验证码等输入框,以及一个“登陆”按钮。
2.验证码实现:为了防止恶意攻击,可以在页面中添加一个验证码输入框,用户需要输入正确的验证码才能进行登陆。验证码可以使用Java图形界面技术生成,并存储在Session中。
3.数据库表设计:创建一个用户表,用于存储用户的登录信息,包括用户名、密码、邮箱等字段。
4.后端代码实现:使用struct2框架编写Action类,并编写一个登陆方法,该方法需要从前端获取输入的用户名、密码、验证码等信息,并检查这些信息是否合法,如果合法则查询数据库,检查用户名和密码是否匹配,如果匹配,则将用户信息存储在Session中,并跳转到主页面。
以下是一个简单的实现代码示例:
1.前端页面设计:login.jsp
<html>
<head>
<title>用户登陆</title>
</head>
<body>
<form action="loginAction" method="post">
用户名: <input type="text" name="username" id="username" /><br />
密码: <input type="password" name="password" id="password" /><br />
验证码: <input type="text" name="verifyCode" id="verifyCode" /><br />
<img src="verifyCode" id="verifyCodeImg" />
<a href="#" onclick="document.getElementById('verifyCodeImg').src='verifyCode?rnd='+Math.random();">看不清,换一张</a><br />
<input type="submit" value="登陆" /><br />
</form>
</body>
</html>
2.验证码实现:verifyCode.jsp
<%@ page import="java.awt.*, java.awt.image.*, java.util.*, javax.imageio.*, java.io.*" %>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=90, height=40;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
String[] codes = {"1","2","3","4","5","6","7","8","9","0","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
String code = "";
for (int i = 0; i < 4; i++)
{
String str = codes[random.nextInt(codes.length)];
code += str;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(str,13*i+15,25);
}
session.setAttribute("verifyCode", code);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
private Color getRandColor(int fc, int bc)
{
Random random = new Random();
if(fc > 255) fc = 255;
if(bc > 255) bc = 255;
int r = fc + random.nextInt(bc-fc);
int g = fc + random.nextInt(bc-fc);
int b = fc + random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
3.数据库表设计:User
CREATE TABLE User(
user_id int(11) NOT NULL auto_increment,
username varchar(20) NOT NULL,
password varchar(32) NOT NULL,
email varchar(50) default NULL,
PRIMARY KEY(user_id)
);
4.后端代码实现:LoginAction.java
public class LoginAction extends ActionSupport {
private String username;
private String password;
private String verifyCode;
public String execute() throws Exception {
if (!verifyCode.equalsIgnoreCase((String)session.getAttribute("verifyCode"))) {
addFieldError("verifyCode", "验证码输入错误");
return INPUT;
}
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "password");
pstmt = conn.prepareStatement("SELECT * FROM User WHERE username=? AND password=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
User user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
session.setAttribute("user", user);
return SUCCESS;
} else {
addActionError("用户名或密码错误");
return INPUT;
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
addActionError("数据库连接失败");
return INPUT;
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
}
// Getters and setters
// ...
}
另外,还需要在struts.xml中配置Action映射:
<package name="default" extends="struts-default">
<action name="loginAction" class="com.example.LoginAction">
<result name="success">/index.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
以上代码仅供参考,还需要进行优化和完善。
阅读全文