JSP编写一个登陆界面

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP编写一个登陆界面相关的知识,希望对你有一定的参考价值。

JSP编写一个登陆界面,连接一个Excel,要求用servlet 最好有截图说明,代码都有的 邮箱:haopeng686@126.com

1、首先准备Dreamweaver8软件,解压安装。如下图所示:这件点击安装程序,然后输入序列号就可以了。

2、在安装软件时候,我们可以看到是否关联【jsp文件】。

3、安装好了软件以后,我们打开Dreamweaver8软件。点击菜单上的【文件】——【新建】。

4、弹出【新建文档】——【动态页】——【jsp】——【创建】。

5、点击【拆分】,在【<body>】标签下面输入:<%     out.println("Hello World!");     %>。

6、然后按快捷键【ctrl+s】保存jsp文件。保存类型jps;。

参考技术A 登录界面很简单的,连接Excel的话用jxl,在servlet和jsp中都可以写连Excel的代码,Sorry,现在我搞C的,Java几年不写了。。。 参考技术B 一、登录页面主要代码:login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<body>
<form id="form1" name="form1" method="post" action="servlet/LoginServlet">
<table width="260" border="1" align="center">
<tr>
<td width="64">用户:</td>
<td width="180"><input type="text" name="username" /></td>
</tr>
<tr>
<td>密码:</td>
<td> <input type="password" name="userpass" /></td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="Submit2" value="登录" />
<input type="reset" name="Submit" value="重置" />
</td>
</tr>
</table>
</form>
</body>
二、对用户登录进行的操作类:loginDao.java
package com.login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.db.dbpool; //数据库连接包
public class loginDao //对用户登录进行的操作类
Connection myCon=null;
PreparedStatement pst=null;
ResultSet rs=null;
//管理员登录验证方法
public boolean CheckLogin(String username,String userpass)

boolean bool=false;
dbpool pool=new dbpool(); //实例化数据库连接
myCon=pool.getConnection(); //调用连接方法
String sql="select * from td_user where uname='"+username+"' and upass='"+userpass+"'";;
try
pst=myCon.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next())

bool=true;

catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();

return bool;


三、登录的servlet主要代码:LoginServlet.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.login.loginDao; //对登录验证操作的类
public class LoginServlet extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
//管理员登录验证
request.setCharacterEncoding("gb2312");
String username=request.getParameter("username"); //获取登录表单数据
String userpass=request.getParameter("userpass");
loginDao lodo=new loginDao(); //声明操作类,调用验证方法
boolean bool=lodo.CheckLogin(username, userpass);
if(bool)
//通过验证,登录成功 跳转到目标页面
response.sendRedirect("index.jsp");
else
response.sendRedirect("login.jsp");


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doGet(request,response);


//------Lz记得给我加分啊 , 不懂得可以在线咨询 呵呵本回答被提问者采纳

02PSP0级及登陆界面开发

一、PSP0级

二、程序设计思想

编写jsp,dao包内加入方法,连接数据库,测试连接。

三、源代码

<form action="add.jsp" method="get">
        <table align="center" border="1" width="500">
            <tr>
                <td> 课程名称: </td>
                <td>
                    <input type="text" name="username" />
                    <%
//                         if(errorMsg != null){
//                             if(errorMsg.get("username") != null){
//                                 out.println( errorMsg.get("username"));
//                             }
//                         }
                        
                    %>
                    
                </td>
            </tr>
                <tr>
                <td>教师名称:</td>
                <td>
                    <input type="password" name="password" />
                    <%
//                         if(errorMsg != null){
//                             if(errorMsg.get("password") != null){
//                                 out.println( errorMsg.get("password"));
//                             }
//                         }
                    
                    %>
                    
                </td>
            </tr>
            <tr>
                <td>上课地点:</td>
                <td>
                    <input type="text" name="nickname" />
                    <%
//                         if(errorMsg != null){
//                             if(errorMsg.get("nickname") != null){
//                                 out.println( errorMsg.get("nickname"));
//                             }
//                         }
                    
                    %>
                
                </td>
            </tr>
            <tr align="center">
                <td colspan="2">
                    <input type="submit" value="提交" />
                    <input type="reset" value="重置" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
  <%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
    //接收客户端传递过来的参数
    String username = request.getParameter("username");
    String password = request.getParameter("passwor");
    String nickname = request.getParameter("nickname");
    if(username == null || "".equals(username.trim())){
        request.setAttribute("error", "用户名不能为空");
    
%>
    <jsp:forward page="addInput.jsp"></jsp:forward>
<%
}
    User user = new User();
    user.setUsername(username);
    user.setPassword(password);
    user.setNickname(nickname);
    
    UserDaoImpl userDao = new UserDaoImpl();
    try{
    userDao.add(user);
%>


    用户保存成功!!<br>
    <a href="addInput.jsp">继续添加</a><br>
    <a href="#">用户列表</a>
<%
    }catch(UserException e){
%>
    <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
    <%
    }
    %>
</html>
package com.jaovo.msg.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import com.jaovo.msg.model.User;

import sun.net.www.content.text.plain;

public class UserDaoImpl implements IUserDao {

    @Override
    public void add(User user) {
        
        Connection connection = DBUtil.getConnection();
        
        String sql = "select count(*) from t_user where username = ?";

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            
            resultSet = preparedStatement.executeQuery();
    
            while(resultSet.next()) {
                if (resultSet.getInt(1) > 0) {
                    throw new UserException("鐢ㄦ埛宸插瓨鍦�") ;
                }
            }
            
            sql = "insert into t_user(username,password,nickname) value (?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getNickname());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        
    }

    @Override
    public void delete(int id) {
        Connection connection = DBUtil.getConnection();
        String sql = "delete from t_user where id = ?";
        PreparedStatement preparedStatement = null;
        
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        
    }

    @Override
    public void update(User user) {
        Connection connection = DBUtil.getConnection();
    
        String sql = "update t_user set password = ? , nickname=? where id = ?";

        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setString(2, user.getNickname());
            preparedStatement.setInt(3, user.getId());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
    }

    @Override
    public User load(int id) {
        Connection connection = DBUtil.getConnection();
        
        String sql = "select * from t_user  where id = ?";
        
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                user = new User();
                user.setId(id);
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("passwor"));
                user.setNickname(resultSet.getString("nickname"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  user;
    }

    @Override
    public User load(String username) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<User> load() {
        Connection connection = DBUtil.getConnection();
    
        String sql = "select * from t_user ";
    
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
    
        List<User> users = new ArrayList<User>();
        User user = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("passwor"));
                user.setNickname(resultSet.getString("nickname"));
                users.add(user);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  users;
    }

}

 

以上是关于JSP编写一个登陆界面的主要内容,如果未能解决你的问题,请参考以下文章

用java做好的登陆界面,当登陆成功后跳转到下个页面的代码是啥?

02PSP0级及登陆界面开发

JSP/Servlet Web 学习笔记 DayThree —— 实现一个登陆小界面

JSP 设计教师与学生不同登陆界面(带验证码)

JSP制作简单登陆

用java写一个登陆界面代码。