jsp用户登录注册界面代码(jsp实现登录注册)

发布时间:2023-12-08

jsp用户登录注册界面代码(jsp实现登录注册)

更新: 2022-11-12 17:56

本文目录一览:

  1. 编写用户注册于登录的JSP页面的全部程序代码
  2. 如何用eclipse写登录注册页面的代码
  3. JSP编写一个登陆界面
  4. 求大神写一下jsp的简单的注册界面代码。

编写用户注册于登录的JSP页面的全部程序代码

3个jsp文件,第一个是login.jsp,第二个是judge.jsp,第三个是afterLogin.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form name="loginForm" method="post" action="judgeUser.jsp">
<table>
<tr>
<td>用户名:<input type="text" name="userName" id="userName"/></td>
</tr>
<tr>
<td>密码:<input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td><input type="submit" value="登录" style="background-color:pink" /> <input type="reset" value="重置" style="background-color:red"/></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>身份验证</title>
</head>
<body>
<%
request.setCharacterEncoding("GB18030");
String name = request.getParameter("userName");
String password = request.getParameter("password");
if(name.equals("abc") && password.equals("123")) {
%>
<jsp:forward page="afterLogin.jsp">
<jsp:param name="userName" value="<%=name%>"/>
</jsp:forward>
<%
} else {
%>
<jsp:forward page="login.jsp"/>
<%
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录成功</title>
</head>
<body>
<%
request.setCharacterEncoding("GB18030");
String name = request.getParameter("userName");
out.println("欢迎你:" + name);
%>
</body>
</html>

如何用eclipse写登录注册页面的代码

java写的用户登录实例,实际页面展示使用的jsp,那么下面是jsp的登录页面代码:

1、login.jsp代码

<%
String name = request.getParameter("username");
String pwd = request.getParameter("password");
//out.println(name+pwd);
String sql ="select * from info where username='"+name+"' and password='"+pwd+"'";
//out.println(sql);
Statement stm= null;
ResultSet rs =null;
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
if(rs.next()) {
session.setAttribute("username",name);
response.sendRedirect("index.html");
} else {
response.sendRedirect("index1.html");
}
} catch(SQLException e) {
e.printStackTrace();
}
%>
<!--登录的表单-->
<form name="form1" method="post" action="login.jsp">
<p>
<label for="username"></label> 用户名
<input type="text" name="username" id="username">
</p>
<p>
<label for="passwrod"></label> 密码
<input type="text" name="passwrod" id="passwrod">
</p>
<p>
<input type="submit" name="button" id="button" value="提交">
</p>
</form>

2、用户信息表,存放用户名和密码:

user_info 表

CREATE TABLE IF NOT EXISTS `test` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `username` char(150) DEFAULT NULL,
  `password` varchar(32),
  `times` int(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

JSP编写一个登陆界面

  1. 首先准备Dreamweaver8软件,解压安装。如下图所示:这件点击安装程序,然后输入序列号就可以了。
  2. 在安装软件时候,我们可以看到是否关联【jsp文件】。
  3. 安装好了软件以后,我们打开Dreamweaver8软件。点击菜单上的【文件】——【新建】。
  4. 弹出【新建文档】——【动态页】——【jsp】——【创建】。
  5. 点击【拆分】,在【body】标签下面输入:<% out.println("Hello World!"); %>
  6. 然后按快捷键【ctrl+s】保存jsp文件。保存类型jps;

求大神写一下jsp的简单的注册界面代码。

1.需要一个jsp页面:

<!-- login.jsp核心代码 -->
<form action="${pageContext.request.contextPath}/servlet/UserServlet" method="post">
<input type="text" name="loginname" />
<input type="password" name="password"/>
<input type="submit" value="登录"/>
</form>

2.需要一个servlet来验证登录信息

//UserServlet 核心代码
class UserServlet extends HttpServlet{
    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        process(request, response);
    }
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        process(request, response);
    }
    private void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PrintWriter pw = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        String loginname = request.getParameter("loginname");
        String password = request.getParameter("password");
        //创建一个service来处理业务逻辑(包括查询数据库操作)
        UserService service = new UserService();
        boolean bool = service.validateUser(loginname,password);
        if(!bool){
            pw.println("用户名或密码错误");
        }else{
            pw.println("登录成功");
        }
    }
}

3.需要一个service处理业务逻辑(包括查询数据库操作)

//UserService 核心代码
public class UserService{
    /**
     * 查询数据库验证用户是否存在,返回boolean
     */
    public boolean validateUser(String loginname,String password){
        boolean bool = false;
        Connection conn = null;
        PreparedStatement ps = null;
        //这里以mysql为例
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
            String sql = "select login_name,pass_word from t_user where login_name=? and pass_word=?";
            ps = conn.prepareStatement(sql);
            ps.setString(0, loginname);
            ps.setString(1, password);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                bool = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if(conn != null){
                    conn.close();
                    conn = null;
                }
                if(ps != null){
                    ps.close();
                    ps = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return bool;
    }
}