jsp登陆界面出错
index.jsp页面程序代码:
<%@ page contentType="text/html; charset=GB2312"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body bgcolor="cyan" background="image/1.bmp"> <h2 align="center"><font style="font-family:'方正舒体';color='red'">登陆界面</font></h2> <form action="LoginServlet" method="post" name="form"> <table border=1 align=center width=200 height=80> <tr align=center> <td><font color=red>用户名</font></td> <td><input type="text" name="username" size="15"></td> </tr> <tr align=center> <td><font color=red>密码</font></td> <td><input type="password" name="password" size="15"/></td> </tr> <tr> <td colspan="2" align=center><input type="submit" value="提交" name="submit"/></td> </tr> </table> </form> </body> </html>servlet
程序代码:
package com.sun.servlet; import import import java.sql.*; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class LoginServlet extends HttpServlet { public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection con = null; Statement sql = null; ResultSet rs = null; try { String name = request.getParameter("username"); String pass = request.getParameter("password"); con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; Database=student", "sa", "123"); sql = con.createStatement(); rs = sql.executeQuery("select user,password from message"); while(rs.next()) { String user = rs.getString(1); String psw = rs.getString(2); if(name.equals(user)&&pass.equals(psw)) { response.sendRedirect("success.jsp"); } } } catch(SQLException e) { e.printStackTrace(); } catch(Exception e) {e.printStackTrace();} finally { try { if(rs!=null)rs.close(); if(rs!=null)sql.close(); if(rs!=null)con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void init(ServletConfig config) throws ServletException { super.init(config); try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch(ClassNotFoundException e) { e.printStackTrace(); } } }报错:
程序代码:
java.lang.NullPointerException at com.sun.servlet.LoginServlet.doPost(LoginServlet.java:49) at javax.servlet.http.HttpServlet.service(HttpServlet.java:710) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.(JIoEndpoint.java:447) at java.lang.Thread.run(Unknown Source)