| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 979 人关注过本帖
标题:[求助]mvc模式下简单用户登陆代码 错误!!
取消只看楼主 加入收藏
frog555
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-11-9
收藏
 问题点数:0 回复次数:1 
[求助]mvc模式下简单用户登陆代码 错误!!

主界面如下:
<%@page contentType="text/html;charset=GBK"%>
<HTML>
<HEAD>
<TITLE>Captain Fresh Seafood Supermarket : Welcome</TITLE>
<SCRIPT LANGUAGE="JavaScript">

function subm()
{
if (form1.username.value.length<3 || form1.password.value.length<3)
{
alert("The length of username can't be less then 3");
return;
}
if ( form1.password.value.length<3)
{
alert("The length of password can't be less then 3");
return;
}
document.form1.submit();
}
function on1()
{
if (form1.username.value == "username")
{
form1.username.value = "";
}
}
function on2()
{
if (form1.password.value == "password")
{
form1.password.value = "";
}
}

</SCRIPT>
</HEAD>

<BODY vLink=#ffcc33 aLink=#ffcc33 link=#993333 bgColor=#ffffff leftMargin=0
topMargin=0 rightMargin=0 MARGINHEIGHT="0" MARGINWIDTH="0">
<center>
<TABLE cellSpacing=0 cellPadding=0 width=650 border=0>
<TBODY>
<TR>
<TD width=172><IMG alt="Captain Fresh Seafood Supermarket"
src="images/home_top_left.jpg"
border=0 width="172" height="62"></TD>
<TD width=253><IMG height=62 alt="Captain Fresh Seafood Supermarket"
src="images/home_top_mid.jpg" width=253
border=0></TD>
<TD width=225><IMG height=62 alt="Captain Fresh Seafood Supermarket"
src="images/home_top_right.jpg" width=225
border=0></TD>
</TR>
</TBODY>
</TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=650
background="images/top_band.gif" border=2>
<TBODY>
<TR>
<TD align=right height=25><FONT face=Verdana,Arial,Helvetica
size=1>February 23, 2004&nbsp;&nbsp;&nbsp;</FONT></TD>
</TR>
</TBODY>
</TABLE>


<TABLE cellSpacing=0 cellPadding=0 width=650 border=0 height="134">
<TBODY>
<TR>
<TD width=99 rowSpan=4 height="134"><IMG alt="Captain Fresh Seafood Supermarket"
src="images/home_mid_left.jpg"
border=0 width="99" height="183"></TD>
<TD width=182 rowSpan=4 height="134"><IMG alt="Captain Fresh Seafood Supermarket"
src="images/home_mid_mid_left.jpg"
border=0 width="182" height="183"></TD>
<TD width=160 rowSpan=4 height="134"><IMG alt="Captain Fresh Seafood Supermarket"
src="images/home_mid_mid_right.jpg"
border=0 width="160" height="183"></TD>
<TD vAlign=top width=185 height="28">
<%
if(request.getParameter("error")==null)
{
out.println("<font size=3 color=black>请输入用户名和密码");
}else
{
out.println("<font size=3 color=red>无效的用户名或密码");
}

%>
</font>
</TD>
<TD width=24 rowSpan=4 height="134"><IMG height=183 alt="Captain Fresh Seafood Supermarket"
src="images/home_mid_right.jpg" width=24
border=0></TD>
</TR>
<TR>
<FORM name="form1" action="LoginServlet" method=post>
<TD vAlign=top width=185 height="27"><font size = 1>username:</font><input type=text value="" size=15 style="color: #CA8F1F" name="username" onfocus="on1()"> </TD>
</TR>
<TR>
<TD vAlign=top width=185 height="27"><font size = 1>password:</font><input type=password value="" size=15 style="color: #CA8F1F" name="password" onfocus="on2()"> </TD>
</TR>
<TR>
<TD vAlign=top width=185 height="52" align="center">
<BR>
<IMG alt="" src="images/dot_clear.gif" border=0><BR>

<img alt=Go src="images/go_button.gif" border=0 width="43" height="23" onclick="subm()">
</TD>
</TR>
</TBODY>
</TABLE>

<TABLE cellSpacing=0 cellPadding=0 width=650 border=0>
<TBODY>
<TR>
<TD vAlign=top width=281><IMG height=132 alt="Captain Fresh Seafood Supermarket"
src="images/home_bot_left.jpg" width=281
border=0></TD>
<TD vAlign=top width=160><IMG height=132 alt="Captain Fresh Seafood Supermarket"
src="images/home_bot_mid.jpg" width=160
border=0></TD>
<TD vAlign=top width=209><IMG height=132 alt="Captain Fresh Seafood Supermarket"
src="images/home_bot_right.jpg" width=209
border=0></TD>
</TR>
<TR>
<TD vAlign=top align=middle colSpan=3><IMG height=1 alt=""
src="images/dot_clear.gif" width=1
border=0><BR><FONT face=Verdana,Arial,Helvetica size=1>copyright ?004
Captain Fresh Seafood Supermarket<BR>designed and programmed by Sika Corp.</FONT>
</FORM>
</TD>
</TR>
</TBODY>
</TABLE>
</center>
</BODY>
</HTML>

login的javabean如下
package login;

import java.sql.*;
import com.jspdev.util.*;

public class LoginBean {
private Connection connection;
//构造方法,获得数据库的连接。
public LoginBean()
{
this.connection = DataBaseConnection.getConnection();
}

public boolean login(String username, String password)throws Exception
{
boolean flag = false;
String sql = "select flag from shop_user where username='"+username+"' and password='"+password+"'";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
flag = rs.wasNull();
return flag;
}
}


LoginServlet的代码如下

package login;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
* Servlet implementation class for Servlet: LoginServlet
*
*/
public class LoginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String target;
boolean flag = false;
String username = request.getParameter("username");
String password = request.getParameter("password");
LoginBean loginbean = new LoginBean();
try {
flag = loginbean.login(username, password);
} catch (Exception e) {
e.printStackTrace();
}

if (flag == true)
{
target = "main.jsp";
}
else
{
target = "index.jsp";
}

request.getRequestDispatcher(target).forward(request, response);
}
}

调了老半天调不出来!~~老说我空指针异常?帮帮忙我错哪错了``学校的小项目``学的头晕了``

搜索更多相关主题的帖子: mvc 模式 代码 用户 登陆 
2007-10-15 20:49
frog555
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-11-9
收藏
得分:0 
补充一下``servlet的xml都配好了
2007-10-15 20:50
快速回复:[求助]mvc模式下简单用户登陆代码 错误!!
数据加载中...
 
   



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

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