我有一个post method to servlet
,所以servlet在将请求转发到另一个页面后返回一个网页,
getServletContext().getRequestDispatcher("/AnotherServlet").forward(request, response);
所以我不能直接重定向我尝试了很多解决方案:使用链接传递参数并且它正在工作,但我会尝试window.location ="/Anotherservlet"
一个更好的并且它正在工作但是页面的 url 仍然是我的主页,即使页面是从服务器下载的,页面仍在加载(所有带有就绪语句的功能都不起作用)所以当我单击带有 href="#" 的按钮时,它会将我重定向到主页,因为正如我在我的 URL=/ not =/anotherservlet 之前所说的那样,我不喜欢执行此操作的 get 方法或 cookie 我搜索其他解决方案感谢帮助
这是我的 servlet:var w = window.open(); w.document.write(data);
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;
}
}
});`