jsp登录界面代码和效果图,jsp用户登录界面代码

发布时间:2022-11-25

本文目录一览:

  1. 用jsp怎样做一个用户登录界面
  2. 求大神写一下jsp的简单的注册界面代码。
  3. jsp登陆界面源代码
  4. JSP编写一个登陆界面
  5. 如何用eclipse写登录注册页面的代码

用jsp怎样做一个用户登录界面

<%@ page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>
<body>
<h1>Register</h1>
<hr />
<form action="login" method="post">
用户名:
<input type="text" name="username" id="text" />
密码:
<input type="password" name="pwd" id="text" />
<input type="submit" value="登录" id="button" />
</form>
</body>

后端处理代码(Servlet)

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AddUser extends HttpServlet {
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        try {
            if (username.equals("name") && pwd.equals("pwd")) {
                System.out.print("登录成功");
            } else {
                System.out.print("error");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是一个简单的登录界面,不需要数据库。如果需要连接数据库,还需要进行更多判断。

求大神写一下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");
        UserService service = new UserService();
        boolean bool = service.validateUser(loginname, password);
        if (!bool) {
            pw.println("用户名或密码错误");
        } else {
            pw.println("登录成功");
        }
    }
}

3. 业务逻辑处理(UserService)

public class UserService {
    public boolean validateUser(String loginname, String password) {
        boolean bool = false;
        Connection conn = null;
        PreparedStatement ps = null;
        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;
    }
}

jsp登陆界面源代码

1. login.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>

2. judge.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>
<%
    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>

3. afterLogin.jsp

<%@ 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>

JSP编写一个登陆界面

  1. 安装 Dreamweaver 8 软件。
  2. 在安装过程中选择是否关联 JSP 文件。
  3. 打开 Dreamweaver 8,点击【文件】——【新建】。
  4. 选择【新建文档】——【动态页】——【JSP】——【创建】。
  5. 点击【拆分】,在 <body> 标签中输入如下代码:
<%
    out.println("Hello World!");
%>
  1. Ctrl + S 保存文件,保存类型为 .jsp

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

1. login.jsp

<%
    String name = request.getParameter("username");
    String pwd = request.getParameter("password");
    String sql = "select * from info where username='" + name + "' and password='" + pwd + "'";
    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="password"></label> 密码
        <input type="password" name="password" id="password" />
    </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;