如题,在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"但是还是不行,有没有人有好办法啊,求救...
out.println("<script language=\"javascript\">" +"alert(\'登陆成功!\')"+"</script>");
response.sendRedirect("chat/communicate.jsp");
但是在提交后,直接跳转到chat/communicate.jsp页面了,没有alert,有人说不用sendRedirect(),直接在script脚本中写
window.location.href="chat/communicate.jsp"但是还是不行,有没有人有好办法啊,求救...
PrintWriter out = response.getWriter();
out.write("<script language='javascript'>alert('登陆成功!');window.location.href='"+request.getContextPath()+"/chat/communicate.jsp';</script>");我试过这样是可以的
//个人意见,仅供参考
PrintWriter out = response.getWriter();
out.println("<script language=\"javascript\">" +"alert(\'登陆成功!\')"+"</script>");
response.setHeader("refresh", "3;url=http://localhost:8080/chat/communicate.jsp");
这个你试试
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();
}
}
form表单:
<form action="../checkUser" method="post" name="myForm1">
<table width="399" border="0" align="center" cellpadding="0"
cellspacing="0" bordercolor="#EBEBEB">
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </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> </strong></span></div>
</td>
</tr>
<tr>
<td height="60" colspan="2" valign="bottom">
<div align="center"><input name="submit" type="submit"
class="d6" value="登 录" /> <input
name="reset" type="reset" class="d6" value="重 置" /></div>
</td>
</tr>
<tr> </tr>
</table>
</form>
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();
} }这是我修改后的,试一下吧
当这个页面在加载的时候 他就会运行这句话了,。
其实,要实现这个功能还是挺简单的,还可以向session里面添加一个属性,让其为String 类型的,把脚本代码以字符串的形式输出,但是,我现在还不知道,为什么这种方法不行,还有他的解决办法是什么?