Java 我的servlet正在抛出异常
我试图构建一个登录面板,但是当我试图从控制器(LoginController)调用模型内部的方法(CustomerButil)时,它抛出异常。我对JSP有些陌生,无法调试它。代码和控制台输出如下所示: 登录表单Java 我的servlet正在抛出异常,java,jsp,servlets,Java,Jsp,Servlets,我试图构建一个登录面板,但是当我试图从控制器(LoginController)调用模型内部的方法(CustomerButil)时,它抛出异常。我对JSP有些陌生,无法调试它。代码和控制台输出如下所示: 登录表单 <form action="LoginController" method="POST" id="login-form"> <header class="text-center"> <h2 class="fo
<form action="LoginController" method="POST" id="login-form">
<header class="text-center">
<h2 class="font-light">Login</h2>
</header>
<input type="hidden" name="command" value="LOAD">
<label>Username : </label>
<input type="text" name="username" placeholder="Username" class="form-control" required /><br>
<label>Password : </label>
<input type="password" name="password" placeholder="Password" class="form-control" required /><br>
<input type="submit" value="Login" class="btn btn-primary" /><br><br>
<span style="color:black">New users</span> <a href="user-register.jsp">Register here</a>
</form>
我不知道我是错过了什么,还是做错了什么。请帮忙。提前感谢!:) 您可以将Springbean注入到servlet中
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContex t(getServletContext());
InterfaceType bean = (InterfaceType)context.getBean("BeanName");
我怀疑CustomerButil没有连接属性,因此在调用时为null。在这一行->客户=客户但是。选择客户(用户名,md5);//在此语句中引发异常。@user3138997。。。但我的eclipse不会弹出任何警告或错误,比如找不到方法之类的。这是一个运行时异常,而不是编译错误。尝试初始化CustomerButil。CustomerButil CustomerButil=新CustomerButil(数据源);(我的错误最初我以为这是一个Spring MVC)您的
catch(异常e){throw new ServletException();}
是第一个问题-这意味着您只能知道“出了什么问题”。您至少应该将其更改为thrownewservletexception(e)
,以便查看原始异常的详细信息。不过,我建议尽可能捕获更具体的异常,并且不要声明只抛出异常的方法,除非您必须这样做。@user3138997。。。谢谢你!!你的解决方案奏效了。我在init块中初始化了CustomerButil,它成功了!谢谢:)
package com.loginpanel.web;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
public class CustomerDbUtil {
private DataSource dataSource;
public CustomerDbUtil(DataSource theDataSource) {
dataSource = theDataSource;
}
public boolean addCustomer(Customer theCustomer) throws Exception {
// Declare the DB objects
Connection con = null;
PreparedStatement myStmt = null;
boolean isExecuted = false;
try{
// Get the connection
con = dataSource.getConnection();
// Write the SQL for adding a student
String sql = "INSERT INTO customers ( "
+ " first_name, last_name, username, md5, country) "
+ "VALUES (?, ?, ?, ?, ?)";
// Prepare the statement
myStmt = con.prepareStatement(sql);
// Add the params
myStmt.setString(1, theCustomer.getCustomerFirstName());
myStmt.setString(2, theCustomer.getCustomerLastName());
myStmt.setString(3, theCustomer.getUsername());
myStmt.setString(4, theCustomer.getMd5());
myStmt.setString(5, theCustomer.getCountry());
// Execute the SQL statement
if(myStmt.execute()){
isExecuted = true;
} else {
isExecuted = false;
}
} catch(Exception e) {
e.printStackTrace();
} finally {
// Clear the JDBC objects
close(con, myStmt, null);
}
return isExecuted;
}
public Customer selectCustomer(String user, String md) throws Exception {
Customer theCustomer = null;
// Get the connection
Connection con = null;
PreparedStatement myStmt = null;
ResultSet myRs = null;
System.out.println("What??");
try {
con = dataSource.getConnection();
// Write the SQL statement
String sql = "SELECT * FROM login_module.customers WHERE username = ? AND md5 = ?";
// Prepare the statement
myStmt = con.prepareStatement(sql);
// Add the parameters
myStmt.setString(1, user);
myStmt.setString(2, md);
// Execute the query
myRs = myStmt.executeQuery();
System.out.println("Hello");
// Fetch the student details
if(myRs.next()) {
int customerId = myRs.getInt("customer_id");
String firstName = myRs.getString("first_name");
String lastName = myRs.getString("last_name");
String user_name = myRs.getString("username");
String md5 = myRs.getString("md5");
String country = myRs.getString("country");
// Create a customer object
theCustomer = new Customer(customerId, firstName, lastName, user_name, md5, country);
} else {
throw new Exception("Could not find the customer");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(con, myStmt, myRs);
}
return theCustomer;
}
public void close(Connection conn, Statement stm, ResultSet rs) throws Exception{
try{
// Close the objects
if(conn!=null) {
conn.close();
}
if(stm!=null) {
stm.close();
}
if(rs!=null) {
rs.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
public List<Customer> getCustomers() throws Exception{
List<Customer> listCustomer = new ArrayList<>();
Connection con = null;
Statement myStmt = null;
ResultSet myRs = null;
try {
// Get the connection
con = dataSource.getConnection();
// Create SQL statements
String sql = "SELECT * FROM login_module.customers";
// Prepare the statement
myStmt = con.createStatement();
// Execute the statement and store it into the ResultSet
myRs = myStmt.executeQuery(sql);
// Fetch the data one-by-one and add it to the list
while(myRs.next()){
int id = myRs.getInt("customer_id");
String firstName = myRs.getString("first_name");
String lastName = myRs.getString("last_name");
String username = myRs.getString("username");
String md5 = myRs.getString("md5");
String country = myRs.getString("country");
Customer theCustomer = new Customer (id, firstName, lastName, username, md5, country);
// Add it to the list
listCustomer.add(theCustomer);
}
} catch ( Exception e ) {
e.printStackTrace();
} finally {
// Close the connection objects
close(con, myStmt, myRs );
}
// Return the list
return listCustomer;
}
}
inside validate
May 13, 2017 7:24:00 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.loginpanel.web.LoginController] in context with path [/loginpanel] threw exception [null] with root cause
javax.servlet.ServletException
at com.loginpanel.web.LoginController.doGet(LoginController.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1504)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1460)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContex t(getServletContext());
InterfaceType bean = (InterfaceType)context.getBean("BeanName");