2 piaotian123 piaotian123 于 2014.09.04 15:14 提问

jquery访问servlet 无法进行页面跳转

js

$(document).ready(function(){
$("#user_login").click(function(){

var username=$("#user_name").val();
var password=$("#user_password").val();
//alert(username1);
//var username=encodeURI(encodeURI(username1));
//var password=encodeURI(encodeURI(password1));

    if(username==""||password==""){
        alert("用户名密码不能为空");
        $("#user_name").val("");
        $("#user_password").val("");
        return false;
    }
    $.ajax({
        type:"post",
        url:"LoginSer",
        //dataType:"json",
        data:{"username":username,"password":password},
        success:function(data){
            alert(1);
            if(data=='success'){
                //alert(1);
                location.href="success.jsp";
            }else {
                location.href="error.jsp";
            }
        }
    })
})

})`

servlet

public class LoginSer extends HttpServlet {

private static final long serialVersionUID = 7381169134016556647L;

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html;charset=utf-8");
    response.setCharacterEncoding("UTF-8");
    request.setCharacterEncoding("utf-8");
    //String uname1=request.getParameter("user_name");//使用action传递
    //String passwd1=request.getParameter("user_password");
    String uname=request.getParameter("username");
    String passwd=request.getParameter("password");
    //String uname=URLDecoder.decode(uname1,"utf-8");
    //String passwd=URLDecoder.decode(passwd1,"utf-8");
    //System.out.println(uname);

    LoginBean user=new LoginBean();
    user.setUsername(uname);
    user.setPassword(passwd);
    LoginCheck cku=new LoginCheck();
    boolean bool=cku.checkUsre(user);
    //session
    HttpSession session=request.getSession(true);
    session.setAttribute("username", user.getUsername());

    PrintWriter out = response.getWriter();//定义out

    String forward;
    if(bool){
        forward="success";
        out.println(forward);
        System.out.print("success");
    }else{
        forward="error";
        out.println(forward);
    }
    //RequestDispatcher rd=request.getRequestDispatcher(forward);//页面跳转
    //rd.forward(request,response);
}

}
`

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!