Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用mysql和xml在jsp中登录页面_Java_Xml_Jsp_Servlets_Servletcontextlistener - Fatal编程技术网

Java 使用mysql和xml在jsp中登录页面

Java 使用mysql和xml在jsp中登录页面,java,xml,jsp,servlets,servletcontextlistener,Java,Xml,Jsp,Servlets,Servletcontextlistener,为什么它会显示这个错误 类型异常报告 message An exception occurred processing JSP page /GGCheckLogin.jsp at line 19 description The server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An

为什么它会显示这个错误

类型异常报告

message An exception occurred processing JSP page /GGCheckLogin.jsp at line 19

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /GGCheckLogin.jsp at line 19

16: 
17: try {
18:     String query = "select * from credentials where login=? and password=?";
19:     PreparedStatement ps = (PreparedStatement) con.prepareStatement(query);
20:     ps.setString(1, login);
21:     ps.setString(2, pw);
22:    ResultSet rs = ps.executeQuery();


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
root cause

javax.servlet.ServletException: java.lang.NullPointerException
    org.apache.jsp.GGCheckLogin_jsp._jspService(GGCheckLogin_jsp.java:95)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
root cause

java.lang.NullPointerException
    org.apache.jsp.GGCheckLogin_jsp._jspService(GGCheckLogin_jsp.java:80)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
这是我的xml页面

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_id" version="3.0">
<context-param>
<param-name>dbname</param-name>
<param-value>ggame</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>pw</param-name>
<param-value>******</param-value>
</context-param>
</web-app>
这是我的jsp页面,我在其中验证用户名和密码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>check-login</title>
</head>
<body>
<%

String login = request.getParameter("Username");
String pw = request.getParameter("password");

Connection con = (Connection)pageContext.getServletContext().getAttribute("conn");

try {
    String query = "select * from credentials where login=? and password=?";
    PreparedStatement ps = (PreparedStatement) con.prepareStatement(query);
    ps.setString(1, login);
    ps.setString(2, pw);
   ResultSet rs = ps.executeQuery();

    if(rs.next()){

        response.sendRedirect("success.jsp");
    }
    else{
        response.sendRedirect("http://www.google.ca");
    }
    }
catch (Exception e) {
    e.printStackTrace();
    throw new ServletException(e);
}
   finally{

    }
      %>

</body>
</html>

检查登录
我不确定在运行项目时是否运行myServletContextListner方法。它显示空指针异常;它可能没有从servletcontext获取连接属性。谢谢你的帮助

PreparedStatement ps = con.prepareStatement("select * from credentials where login=? and password=?");
ps.setString(1, login);
ps.setString(2, pw);
ResultSet rs = ps.executeQuery();
您不需要PreparedStatement的强制转换,只需导入java.sql.PreparedStatement

试试这个

public void contextInitialized(ServletContextEvent event){
....
event.getServletContext().setAttribute("conn",conn);
....
}
在jsp中:

<% ServletContext sc = request.getServletContext();
 sc.getAttribute("conn"); %>


但我们无法将字符串转换为prepareStatement'PreparedStatement ps=“select*from credentials,其中login=?和password=?”;'我忘了一件事,对不起
PreparedStatement ps=con.prepareStatement(“从凭证中选择*,其中登录名=?和密码=?”)我尝试了这个,但没有处理相同的错误。。。。。。。它在这一行给出nullpointerException…已编辑。尝试使用该代码,它应该可以工作!顺便说一句,您应该在Class.method中执行查询,而不是在jspnope中执行。它不工作……我认为contextInitialized方法中存在问题,它可能没有设置连接的属性,当我们获取属性时,它返回null
<% ServletContext sc = request.getServletContext();
 sc.getAttribute("conn"); %>