猿类不苟且 2017-11-21 15:05 采纳率: 100%
浏览 7714
已采纳

使用java web 实现登录功能,servlet跳转jsp失败,页面出现空白,求大神解决!!!

package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import dbc.JdbcUtil;

public class UserDao {

public boolean register(String username,String userpw,String identity)throws Exception{

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;


        conn = JdbcUtil.getConnection();
        String sql = "select * from user where username=? and userpw=? and identity=?";
        ps = conn.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2, userpw);
        ps.setString(3, identity);
        rs = ps.executeQuery();
        if (rs.next()) return true;
        else return false;
        }

}

`package dbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtil {
private static String driver;
private static String url;
private static String user;
private static String password;
private static Properties pr = new Properties();

private JdbcUtil() {
}

// ��Ƹù�����ľ�̬��ʼ�����еĴ��룬�ô�����װ����ʱִ�У���ִֻ��һ��
static {
    try {
        pr.load(JdbcUtil.class.getClassLoader().getResourceAsStream(
                "db.properties"));
        driver = pr.getProperty("driver");
        url = pr.getProperty("url");
        user = pr.getProperty("user");
        password = pr.getProperty("password");
        Class.forName(driver);
    } catch (Exception e) {
        throw new ExceptionInInitializerError(e);
    }
}

// ��ƻ�����Ӷ���ķ���getConnection()
public static Connection getConnection() throws SQLException {
    return DriverManager.getConnection(url, user, password);
}

// ����ͷŽ���������ӵķ���free()
public static void free(ResultSet rs, Statement st, Connection conn) {
    try {
        if (rs != null) {
            rs.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (st != null) {
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {

            }
        }
    }
}

}

```package servlets;

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 dao.UserDao;
import vo.User;
import java.sql.*;
public class RegistServlet extends HttpServlet {

/**
 * 
 */
private static final long serialVersionUID = 1L;


/**
 * Constructor of the object.
 */
public RegistServlet() {
    super();
}

/**
 * Destruction of the servlet. <br>
 */
public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
}

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doPost(request,response);
}

/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String un=request.getParameter("username");
    String uw=request.getParameter("userpw");
    String iden=request.getParameter("identity");
    User uu=new User(un,uw,iden);
    UserDao c1 = null;
    PrintWriter out = null;

    try {
        if(c1.register(uu.getUsername(), uu.getUserpw(), uu.getIdentity()))
            request.getRequestDispatcher("/output.jsp").forward(request, response);
        else
            out.print("<script> alert(\"登录失败!\"); </script>");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }


        // TODO Auto-generated catch block



/**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
    // Put your code here
}

}

```package vo;

public class User {
private String username;
private String userpw;
private String identity;
public User(String username, String userpw, String identity) {
this.identity=identity;
this.username=username;
this.userpw=userpw;
// TODO Auto-generated constructor stub
}
public String getUsername(){return username;}
public String getUserpw(){return userpw;}
public String getIdentity(){return identity;}
public void setUsername(String username){this.username=username;}
public void setUserpw(String userpw){this.userpw=userpw;}
public void setIdentity(String identity){this.identity=identity;}
}

url=jdbc:mysql://localhost:3306/LibSytem?useUnicode=true&characterEncoding=utf-8
user=root
password=960714

```<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head><title>登录页面</title> </head>
  <body>
<center><h1>图书馆用户登录</h1></center>
<center>
  <hr width="100%" size="1" color="black">
<form action="RegistServlet" method="post">
<table>
<tr><td>登录名:</td><td><input type="text" name="username"></td></tr>
<tr><td>登录密码:</td><td><input type="password" name="userpw"></td></tr>
<tr><td>身份:</td><td><select name="identity">
<option selected>用户</option>
<option>管理员</option>
</select></td></tr>
<tr><td><input type="submit" value="登录"></td></tr>

</table>
</form>
</center>
</body>
</html>





```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>成功</title>

  </head>

  <body>
    This is my JSP page. <br>
  </body>
</html>


``














  • 写回答

3条回答

  • penguinDada 2017-11-22 02:16
    关注

    这种问题打几个断点调试一下就可以了,看你那个登录的jsp表单提交的action路径不对啊,配置的servlet确定是这样子的?应该连servlet都没请求到吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况