| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 843 人关注过本帖
标题:jsp登陆界面出错
只看楼主 加入收藏
mybbs
Rank: 1
等 级:新手上路
帖 子:12
专家分:8
注 册:2011-5-8
结帖率:100%
收藏
 问题点数:0 回复次数:3 
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)

搜索更多相关主题的帖子: html background starting 方正舒体 
2011-09-21 20:51
baojin
Rank: 2
等 级:论坛游民
帖 子:28
专家分:73
注 册:2009-10-12
收藏
得分:0 
楼主能把数据库相关的表发下么,顺便问句你是本地访问的还是发布在网上访问的?
关于楼主的包com.sun.servlet,我觉得最好改下,发布到网上可能会有冲突的哦,这样
的习惯不好
2011-09-22 14:30
mybbs
Rank: 1
等 级:新手上路
帖 子:12
专家分:8
注 册:2011-5-8
收藏
得分:0 
问题已解决,谢谢参与。
2011-09-22 17:48
gwl3323405
Rank: 2
等 级:论坛游民
帖 子:22
专家分:46
注 册:2010-10-31
收藏
得分:0 
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");这一句为什么放在init方法里面呢,你要保证dopost方法调用前就执行这一句。
if(rs!=null)sql.close();和if(rs!=null)con.close();我看不懂,statement和Connection的关闭和rs等不等于null有什么关系,写错了吧!?
2011-10-04 13:07
快速回复:jsp登陆界面出错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028126 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved