如题,在servlet写一句话:PrintWriter out=response.getWriter();
out.println("<script language=\"javascript\">" +"alert(\'登陆成功!\')"+"</script>");
response.sendRedirect("chat/communicate.jsp");
但是在提交后,直接跳转到chat/communicate.jsp页面了,没有alert,有人说不用sendRedirect(),直接在script脚本中写
window.location.href="chat/communicate.jsp"但是还是不行,有没有人有好办法啊,求救...

解决方案 »

  1.   

    在前面加上这句试一下response.setContentType("text/html;charset=utf-8");
      

  2.   

    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();
    out.write("<script language='javascript'>alert('登陆成功!');window.location.href='"+request.getContextPath()+"/chat/communicate.jsp';</script>");我试过这样是可以的
      

  3.   


    //个人意见,仅供参考
    PrintWriter out = response.getWriter();
    out.println("<script language=\"javascript\">" +"alert(\'登陆成功!\')"+"</script>");
    response.setHeader("refresh", "3;url=http://localhost:8080/chat/communicate.jsp");
      

  4.   

    sendRedirect重定向页面会清空Out的缓冲区...所以前面那条指令无效了..
      

  5.   

    out.println("<script>alert('登陆成功!');window.location.href('chat/communicate.jsp');</script>");
    这个你试试
      

  6.   

    servlet:
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setCharacterEncoding("utf-8");
    response.setContentType("utf-8");
    request.setCharacterEncoding("utf-8");

    PrintWriter out = response.getWriter();
    HttpSession session=request.getSession(true);
    ServletContext application=this.getServletContext();

    List<HttpSession> userSession=(List<HttpSession>)application.getAttribute("userSession");
    if(userSession==null || userSession.size()==0){
    userSession=new ArrayList<HttpSession>();
    }
    boolean flag=false;
    for (int i = 0; i < userSession.size(); i++) {
    if(userSession.get(i)==null){
    userSession.remove(i);
    }
    if(userSession.get(i).equals(session)){
    flag=true;
    }
    }
    if(!flag){
    userSession.add(session);
    }
    application.setAttribute("userSession", userSession);


    String userName=request.getParameter("userName");
    String password=request.getParameter("password");
    BaseDao dao=new BaseDao();
    String sql="select * from users where username='"+userName+"'";
    ResultSet rs=null;
    rs=dao.querySql(sql,null);
    try {
    if(rs.next()){
    if(rs.getString("password").equals(password)){
    session.setAttribute("user", userName);
    int sex=rs.getInt("sex");
    session.setAttribute("sex", sex);
    out.println("<script language=\"javascript\">" +"alert(\'登陆成功!\')"+"</script>");
    response.setHeader("refresh", "3;url=http://localhost:8080/company11/chat/communicate.jsp");
    // out.write("<script language='javascript'>alert('登陆成功!');window.location.href='"+
    // request.getContextPath()+"/chat/communicate.jsp';</script>");
    response.sendRedirect("chat/communicate.jsp");
    }else{
    out.println("<script language=\"javascript\">" +"alert(\'密码错误!\')"+"</script>");
    response.setHeader("refresh", "3;url=http://localhost:8080/company11/chat/index.jsp");
    // out.write("<script language='javascript'>alert('密码错误!');window.location.href='"+
    // request.getContextPath()+"/chat/index.jsp';</script>");
    response.sendRedirect("chat/index.jsp");

    }
    }else{
    out.println("<script language=\"javascript\">" +"alert(\'用户名错误或者密码错误!\')"+"</script>");
    response.setHeader("refresh", "3;url=http://localhost:8080/company11/chat/register.jsp");
    // out.write("<script language='javascript'>alert('用户名错误或者密码错误!');window.location.href='"+
    // request.getContextPath()+"/chat/register.jsp';</script>");
    response.sendRedirect("chat/register.jsp");
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }finally{
    dao.closeAll();
    }


    }
      

  7.   


    form表单:
    <form action="../checkUser" method="post" name="myForm1">
    <table width="399" border="0" align="center" cellpadding="0"
    cellspacing="0" bordercolor="#EBEBEB">
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr align="left">
    <td height="35" colspan="2" bgcolor="#EAF0FB" class="p16">
    <div align="center"><span class="d6"><strong>会 员
    登 录 </strong></span></div>
    </td>
    </tr> <tr>
    <td width="141" height="45" align="center" bgcolor="#EBEBEB"
    class="d5">您的帐号:</td>
    <td width="258" align="left" bgcolor="#EBEBEB"><input
    name="userName" type="text" /></td>
    </tr>
    <tr> </tr>
    <tr>
    <td height="45" align="center" bgcolor="#EBEBEB" class="d5">
    您的密码:</td>
    <td align="left" bordercolor="#EBEBEB" bgcolor="#EBEBEB"><input
    name="password" type="password" size="21" /></td>
    </tr>
    <tr align="left">
    <td height="35" colspan="2" align="right" bgcolor="#EAF0FB"
    class="p16">
    <div align="right"><span class="d6"><strong><a
    href="register.jsp" tppabs="http://localhost:8080/company/chat/register.jsp" class="dong02">我要注册</a>&nbsp;</strong></span></div>
    </td>
    </tr>
    <tr>
    <td height="60" colspan="2" valign="bottom">
    <div align="center"><input name="submit" type="submit"
    class="d6" value="登  录" /> &nbsp;&nbsp;&nbsp;&nbsp; <input
    name="reset" type="reset" class="d6" value="重  置" /></div>
    </td>
    </tr>
    <tr> </tr>
    </table>
    </form>
      

  8.   

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //response.setCharacterEncoding("utf-8");
    //response.setContentType("utf-8");
    response.setContentType("text/html;charset=utf-8");
    request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter();
    HttpSession session = request.getSession(true);
    ServletContext application = this.getServletContext(); List<HttpSession> userSession = (List<HttpSession>) application
    .getAttribute("userSession");
    if (userSession == null || userSession.size() == 0) {
    userSession = new ArrayList<HttpSession>();
    }
    boolean flag = false;
    for (int i = 0; i < userSession.size(); i++) {
    if (userSession.get(i) == null) {
    userSession.remove(i);
    }
    if (userSession.get(i).equals(session)) {
    flag = true;
    }
    }
    if (!flag) {
    userSession.add(session);
    }
    application.setAttribute("userSession", userSession); String userName = request.getParameter("userName");
    String password = request.getParameter("password");
    BaseDao dao = new BaseDao();
    String sql = "select * from users where username='" + userName + "'";
    ResultSet rs = null;
    rs = dao.querySql(sql, null);
    try {
    if (rs.next()) {
    if (rs.getString("password").equals(password)) {
    session.setAttribute("user", userName);
    int sex = rs.getInt("sex");
    session.setAttribute("sex", sex);
    /*
     * out.println("<script language=\"javascript\">" +
     * "alert(\'登陆成功!\')" + "</script>");
     * response.setHeader("refresh",
     * "3;url=http://localhost:8080/company11/chat/communicate.jsp"
     * );
     */
    out.write("<script language='javascript'>alert('登陆成功!');window.location.href='"
    + request.getContextPath()
    + "/chat/communicate.jsp';</script>");
    // response.sendRedirect("chat/communicate.jsp");
    } else {
    /*
     * out.println("<script language=\"javascript\">" +
     * "alert(\'密码错误!\')" + "</script>");
     * response.setHeader("refresh",
     * "3;url=http://localhost:8080/company11/chat/index.jsp");
     */
    out.write("<script language='javascript'>alert('密码错误!');window.location.href='"
    + request.getContextPath()
    + "/chat/index.jsp';</script>");
    // response.sendRedirect("chat/index.jsp"); }
    } else {
    /*
     * out.println("<script language=\"javascript\">" +
     * "alert(\'用户名错误或者密码错误!\')" + "</script>");
     * response.setHeader("refresh",
     * "3;url=http://localhost:8080/company11/chat/register.jsp");
     */
    out.write("<script language='javascript'>alert('用户名错误或者密码错误!');window.location.href='"
    + request.getContextPath()
    + "/chat/register.jsp';</script>");
    // response.sendRedirect("chat/register.jsp");
    }
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    dao.closeAll();
    } }这是我修改后的,试一下吧
      

  9.   

    不行,在servlet中不能输出,如果没有response.sendRedirect()。那么就会弹出一个下载窗口,内容是out.print里面的内容
      

  10.   

    你可以写一个window.onload=function(){alert("登录成功!");)
    当这个页面在加载的时候  他就会运行这句话了,。
      

  11.   


    其实,要实现这个功能还是挺简单的,还可以向session里面添加一个属性,让其为String 类型的,把脚本代码以字符串的形式输出,但是,我现在还不知道,为什么这种方法不行,还有他的解决办法是什么?