给你详细讲一下。
创新互联公司是专业的泗阳网站建设公司,泗阳接单;提供成都网站设计、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行泗阳网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
比如系统有个登陆页面(login.jsp):
name:_________
password:____________
(登陆按钮)
你按下登陆按钮,就根据name和password去数据库里面查,如果判断有此用户并且密码正确,就设置一个session的键对应的值,键名字自己取,统一即可,比如"userInfo",代码就是servlet的doPost里面
HttpSession session = request.getSession();
Hashtable userInfo = new Hashtable();
userInfo.setAttribute("userName", request.getParameter("userName");
userInfo.setAttribute("passWords", Util.toSecret( request.getParameter("passWords)); //密码最好加密
session.setAttribute("userInfo", userInfo);
session是在一定时期(超时时间内)一直存在的,这段时间内你可以随时判断用户是否合法,否则就退回登陆页面。
在任何除了登陆页面以外的页面访问,只需判断有没有这个键值,没就到登陆页面,否则进正常页面。(最好写在servlet中,让servlet当页面控制器)代码如下:
if ( session.getAttribute("userInfo")==null ) {
response.sendRedirect(request.getServletContext.getPath() + "/login.jsp");
} else {
request.getRequestDispatcher("/正常页面.jsp").forward(request,response);
}
public class LoginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet{//构造函数public LoginServlet(){super();}//服务器端的验证、提交处理 protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{response.setContentType("text/html;charset=GB2312"); MD5_Encoding md5=new MD5_Encoding(); // 获取提交的用户名: String UserName = request.getParameter("logname"); // 获取提交的密码: String Password = request.getParameter("logpwd"); // 对密码进行MD5数据加密 String EnPwd =md5.getMD5ofStr(Password); String random = request.getParameter("random"); String random2 = request.getSession().getAttribute("random").toString(); if (!random.equals(random2)){request.setAttribute("error", "输入的验证码不正确!");try{request.getRequestDispatcher("Login.jsp").forward(request, response);}catch (Throwable t){getServletContext().log(t.getMessage());}}//核对用户名和密码,决定是否通过登陆验证 DB_User dbuser = new DB_User(); User user = dbuser.get(UserName, EnPwd); if (user != null){// 登录成功时,把strUserName设成session变量UserName request.getSession(true).setAttribute("username", UserName);try{request.getRequestDispatcher("Welcome1.jsp").forward(request,response);}catch (Throwable t){getServletContext().log(t.getMessage());}}else{// 否则为游客身份 request.getSession(true).setAttribute("username", UserName);try{request.getRequestDispatcher("Welcome1.jsp").forward(request, response);}catch (Throwable t){getServletContext().log(t.getMessage());}}}}一年前写的了 提交后,在JSP中再对username判断一下就OK了
package test;
import java.util.Scanner;
public class TestLogin {
private static final String USERNAME = "Tom";//此处定义用户名
private static final String PASSWORD = "123";//定义密码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入您的用户名:");
String username_in = sc.next();
System.out.print("请输入您的密码:");
String password_in = sc.next();
if(username_in.equals(USERNAME) password_in.equals(PASSWORD)){
System.out.println("登录成功");
}else{
System.out.println("用户名或密码错误");
}
}
}
这是从控制台输入的最简单的方法。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test_Login extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton bntLogin;
private JButton bntCannel;
private JPasswordField pwd;
private JTextField username;
private JLabel jLabel2;
private JLabel jLabel1;
public static void main(String[] args) {
Test_Login inst = new Test_Login();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
public Test_Login() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jLabel1 = new JLabel();
jPanel1.add(jLabel1);
jLabel1.setText("用户名");
jLabel1.setBounds(45, 30, 75, 25);
}
{
jLabel2 = new JLabel();
jPanel1.add(jLabel2);
jLabel2.setText("密码");
jLabel2.setBounds(45, 75, 55, 15);
}
{
username = new JTextField();
jPanel1.add(username);
username.setBounds(100, 30, 140, 25);
}
{
pwd = new JPasswordField();
jPanel1.add(pwd);
pwd.setBounds(100, 70, 140, 25);
}
{
bntLogin = new JButton();
jPanel1.add(bntLogin);
bntLogin.setText("登陆");
bntLogin.setBounds(40, 120, 60, 30);
bntLogin.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (username.getText().equals("lisong")
pwd.getText().equals("lisong")) {
JOptionPane.showMessageDialog(Test_Login.this,
"登录成功");
} else {
JOptionPane.showMessageDialog(Test_Login.this,
"登录失败");
}
}
});
bntCannel = new JButton();
jPanel1.add(bntCannel);
bntCannel.setText("取消");
bntCannel.setBounds(180, 120, 60, 30);
bntCannel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
}
}
pack();
setSize(300, 215);
} catch (Exception e) {
e.printStackTrace();
}
}
}