错误信息:四月 26, 2016 2:15:22 下午 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() for servlet [LoginServlet] in context with path [/MessageBoard] threw exceptionjava.lang.NullPointerException at servlet.LoginServlet.doPost(LoginServlet.java:61) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) 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:212) 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.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)username = lisi password= 123 params长度:2代码如下:1. servlet包下的package servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import service.LoginService;/** * Servlet implementation class LoginServlet */@WebServlet("/LoginServlet")public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; private LoginService service; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String path = request.getContextPath(); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String password = request.getParameter("password"); List<String> params = new ArrayList<String>(); params.add(username); params.add(password); System.out.println("username = " +username+ " password= " + password +" params长度:"+params.size()); boolean flag=service.loginUser(params); if(flag) { response.sendRedirect(path+"/login_success.jsp"); out.println("成功"); } else { response.sendRedirect(path+"/login_failure.jsp"); out.println("失败"); } } public void init() throws ServletException { // Put your code here }}2.接口package service;import java.util.List;public interface LoginService { public boolean loginUser(List<String> params);}3.实现接口package dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.List;import service.LoginService;import util.JdbcUtils;public class UsersDao implements LoginService{ public boolean loginUser(List<String> params){ PreparedStatement stmt = null; ResultSet rs = null; boolean flag = false; //Connection conn; try { Connection conn = JdbcUtils.getConnection(); String sql = "select * from zoey_oper where usrname=? and password=?"; // SQL语句 stmt = conn.prepareStatement(sql); int index=1; if(params != null && !params.isEmpty()) { for(int i=0; i<params.size(); i++){ stmt.setString(index++, params.get(i)); System.out.println(params.get(i)); } rs = (ResultSet) stmt.executeQuery(); if (rs.next()) { flag = true; rs.close(); stmt.close(); } else { flag = false ; rs.close(); stmt.close(); } conn.close(); return flag; } } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return flag; }}
添加回答
举报
0/150
提交
取消