我有一个post method to servlet
,因此servlet在将请求转发到另一个页面后返回一个网页
getServletContext().getRequestDispatcher("/AnotherServlet").forward(request, response);
所以我无法直接重定向window.location ="/Anotherservlet"
我尝试了很多解决方案:通过链接传递参数并且它正在工作但我会更好一个我试过var w = window.open(); w.document.write(data);
它是工作,但页面的网址仍然是我的主页,页面仍然加载甚至页面从服务器下载(所有功能与ready语句不工作)所以当我点击按钮与href =“#”它重定向到主页因为正如我在我的网址前说的那样= / not = / anotherervlet
我不喜欢得到方法或者为我寻找其他解决方案的cookie感谢您的帮助
这是我的servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name= request.getParameter("name");
String pass=request.getParameter("pass");
if (name.equals("a")&&pass.equals("0cc175b9c0f1b6a831c399e269772661"))
{
getServletContext().getRequestDispatcher("/BookListServlet").forward(request, response);
}
else
{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.print("error");
out.flush();
out.close();
}
}
这是我的BookListServlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
books= new bookiml().get_books();
String name= request.getParameter("name");
String ipAddress = request.getRemoteAddr();
request.setAttribute("books", books);
request.setAttribute("userip", ipAddress);
request.setAttribute("user", name);
request.setAttribute("title", "Book listing");
getServletContext().getRequestDispatcher("/META-INF/pages/book-list.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
我在检查登录验证后不想从servlet获取页面,但是没有在链接中传递参数,如下所示:`
$.ajax({
url: 'identification',
type: 'POST',
data:{pass:$hash,name:$name},
timeout: 1000,
success: function(data){
var target = document.getElementById('spinner');
spinner = new Spinner(opts).spin(target);
$('#spinner').fadeIn();
if (data=='error')
{
$("#messages").html(" Check your username and password.");
$('#name').css('border-color','#B94A48');
$('#pass').css('border-color','#B94A48');
$('#spinner').fadeOut();
}
else
{
$("#messages").html("");
$('#spinner').fadeOut();
window.location ="/BookListServlet?name="+$name;
}
}
});`
答案 0 :(得分:0)
据我了解,您只想使用servlet
重定向?
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", "http://splinky.com/");
答案 1 :(得分:0)
您可以将url放在ajax响应中,以便在成功的情况下进行重写:
示例js响应:
{ status: 'ok', redirect: '/example' }
在客户端使用js重定向。
客户端代码:
$("form.ajaxForm").submit( function(e) {
var form = $(this);
form.ajaxSubmit({
dataType: 'json',
success: function(data) {
if( data.redirect != "" ) {
window.location = data.redirect;
}
}
});
return false;
});
你必须使用jQuery表单插件才能工作。请参阅:http://jquery.malsup.com/form/#getting-started