我写了一个用户登录,但有些地方不知道该怎么写语法,请帮忙看看好吗?
我写了一个用户登录,但有些地方不知道该怎么写语法,请帮忙看看好吗?
我想要如果验证成功就到成功的页面,如果不成功就到失败页面.
public ActionForward login(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) {
XsBean bean = new XsBean();
int flag;
try {
String id = req.getParameter( "id ");//用户名
String mm = req.getParameter( "mm ");//密码
flag=bean.checkUser(id, mm);
if (flag == 1) {
req.getSession().setAttribute( "userName ", id);
String a = (String) req.getSession().getAttribute( "userName ");
System.out.println( "a is : " + a);
}
else
{
//这里要怎么写
}
}
catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward( "login ");
}
[解决办法]
if (flag == 1) {
req.getSession().setAttribute( "userName ", id);
String a = (String) req.getSession().getAttribute( "userName ");
System.out.println( "a is : " + a);
return mapping.findForward( "login ");
}
else
{
return mapping.findForward( "fail ");
}
[解决办法]
看来楼主是很新的手啊,我给你解
public ActionForward login(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) {
XsBean bean = new XsBean();
int flag;
try {
//我加的定义转向
ActionForward forward = new ActionForward();
//我加的,墨认失败
forward = mapping.findForward( "loginfailing ");
String id = req.getParameter( "id ");//用户名
String mm = req.getParameter( "mm ");//密码
flag=bean.checkUser(id, mm);
if (flag == 1) {
req.getSession().setAttribute( "userName ", id);
String a = (String) req.getSession().getAttribute( "userName ");
System.out.println( "a is : " + a);
//我加的,如果成功
forward mapping.findForward( "loginsuccess ");
}
else
{
//这里不用写什么了,也可以写,墨认失败
//这里要怎么写
}
}
catch (Exception e) {
//这里不用写什么了,也可以写,墨认失败
e.printStackTrace();
}
return forward ;
}
你提示没返回是因为每个分支都要定义返回的,你if else定义了,但是catch中没定义