大家帮我这初学者看这个问题!!!

tiandiqing 2004-10-19 05:47:44
//代码如下

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.RequestDispatcher;

public class login extends HttpServlet
{
private static final String CONTENT_TYPE="text/html;charset=GBK";

public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
resp.setContentType(CONTENT_TYPE);
PrintWriter out = resp.getWriter();

out.println("<html>");
out.println("<head><title>用户登陆</title></head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");

out.println("<body>");

String logname,logpass,sqlstr;
boolean loginAttemp = false;
boolean loginOK = false;

String errorMessage = "请您登陆";

HttpSession session = req.getSession(true); ///////////////////
if (req.getParameter("logname")!=null && req.getParameter("logpass")!=null)
{
loginAttemp = true;
}

if (loginAttemp)
{
logname = req.getParameter("logname");
logpass = req.getParameter("logpass");


DBConn myconn = new DBConn();
if (this.checklogin(logname,logpass))
{
errorMessage = "登陆成功";
session.setAttribute("username",logname);
loginOK = true;

if (loginOK)
{
String url = "../result.jsp?username=foo&password=bar";
req.setAttribute("firstName",logname);
req.setAttribute("lastName",logpass);

RequestDispatcher dispatcher = req.getRequestDispatcher(url);
dispatcher.forward(req,resp);

}
}else
{
errorMessage = "登陆不成功";
session.setAttribute("username","");
//resp.sendRedirect("../login.jsp?err="+errorMessage);
out.println(errorMessage);
}
}
out.println("</body>");
out.println("</html>");
}



//检查用户登陆
public boolean checklogin(String username,String password)
{
try{
DBConn loginConn = new DBConn();
loginConn.executeQuery("select * from member where logname='" + username + "'");
if(loginConn.rs_next())
{
return true;
}else
{
return false;
}


}catch(Exception e)
{
return false;
}
}


//中文编码处理

public String getstr(String str)
{
try{
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ISO8859-1");
String temp = new String (temp_t);
return temp;
}catch(Exception e)
{
}
return "null";

}

public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
doGet(req,resp);
}



}

我想我得语句就是错在这里了,


String url = "../result.jsp?username=foo&password=bar";
req.setAttribute("firstName",logname);
req.setAttribute("lastName",logpass);

RequestDispatcher dispatcher = req.getRequestDispatcher(url);
dispatcher.forward(req,resp);

请问转向这里哪写错了????

...全文
73 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
禽兽v5 2004-10-19
不客气!
回复
tiandiqing 2004-10-19
我按你给我的方法就执行了,谢谢了!
回复
禽兽v5 2004-10-19
报的什么错?
回复
tiandiqing 2004-10-19
那我的那个是错在哪里了呀
帮忙看看好吗?
回复
禽兽v5 2004-10-19
带request跳转。

如果要加参数,用如下方法添加
request.setAttribute("name", value);
在目的页面中,用如下方法获取
request.getAttribute("name");

request.getRequestDispatcher("目的页面.jsp").forward(request, response);
回复
tiandiqing 2004-10-19
sendRedirect 方法我知道的,我就是想问问
RequestDispatcher dispatcher = req.getRequestDispatcher(url);
dispatcher.forward(req,resp);

这个怎么个用法!!
回复
joey_lg 2004-10-19
给你个IBM例程中的标准转向:resp.sendRedirect("/PageInput/Servlet2InputPage.html");
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告