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<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>
程序代码:
package com.sun.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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();
}
}
}
报错:import java.io.IOException;
import java.io.PrintWriter;
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.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
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.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
搜索更多相关主题的帖子:
html background starting 方正舒体
----------------解决方案--------------------------------------------------------
楼主能把数据库相关的表发下么,顺便问句你是本地访问的还是发布在网上访问的?
关于楼主的包com.sun.servlet,我觉得最好改下,发布到网上可能会有冲突的哦,这样
的习惯不好
----------------解决方案--------------------------------------------------------
问题已解决,谢谢参与。
----------------解决方案--------------------------------------------------------
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");这一句为什么放在init方法里面呢,你要保证dopost方法调用前就执行这一句。
if(rs!=null)sql.close();和if(rs!=null)con.close();我看不懂,statement和Connection的关闭和rs等不等于null有什么关系,写错了吧!?
----------------解决方案--------------------------------------------------------