在我的项目中,我禁止用户每个页面只有他已经登录.这就是我写下面代码的原因.当我输入浏览器时,例如http:// localhost:8080/JSP1/Students,我来到login.jsp页面.但在输入loginid和密码后,只显示空白页http:// localhost:8080/JSP1/Logged,GlassFish表示存在异常
if (userPath.equals("/Students")){
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
requestDispatcher.forward(request, response);
}
java.lang.IllegalStateException: PWC1227: Cannot forward after response has been committed
Run Code Online (Sandbox Code Playgroud)
完整的doGet和doPost代码:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession ses = request.getSession();
String login = (String)ses.getAttribute("login");
String password = (String)ses.getAttribute("password");
if ((login==null)|(password==null)){
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
requestDispatcher.forward(request, response);
}
//Now we think that we are successfully logged in
String userPath = request.getServletPath();
// System.out.println(userPath);
if (userPath.equals("/Login")){
RequestDispatcher requestDispatcher = …Run Code Online (Sandbox Code Playgroud) 我只想在发生异常时在错误页面上打印自定义消息.
我试过这个
if(erroroccured){
FacesMessage message=new FacesMessage("You must login to continue");
context.addMessage(null, message);
FacesContext.getCurrentInstance().getExternalContext().redirect("error.xhtml");
}
Run Code Online (Sandbox Code Playgroud)
在error.xhtml我给了
<h:messages></h:messages>
Run Code Online (Sandbox Code Playgroud)
标签也..每当发生异常时我的页面都被完美地重定向.但我得到任何错误消息.
我是Java EE世界的初学者.我一直在尝试使用Servlets和JSP创建一个简单的登录系统,遵循此处提供的指南http://come2niks.com/?p=1589.这就是我的doPost()的样子.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
processRequest(request, response);
try
{
System.out.println("In the Login Servlet");
LoginBean user = new LoginBean();
user.setUserName(request.getParameter("username"));
user.setPassword(request.getParameter("pass"));
LoginDAO db = new LoginDAO();
user = db.login(user);
System.err.println("I am Back !");
if(user.isValid())
{
System.err.println("VALIDED.. ReDirecting..");
System.err.println("Getting Session");
HttpSession session = request.getSession(true);
System.err.println("Got Session");
session.setAttribute("currentSessionUser",user);
System.err.println("Attribute Set");
response.sendRedirect("Login_Success.jsp");
}else
{
System.err.println(" NOT VALIDED.. ReDirecting..");
response.sendRedirect("Login_Failed.jsp");
out.println(" NOT VALIDED.. ReDirecting..");
}
} catch (Throwable exc)
{
System.out.println(exc); …Run Code Online (Sandbox Code Playgroud) 它们是否需要在每次查询后关闭并在每次查询开始时初始化?
我知道在getter和setter中编写业务逻辑是一种非常糟糕的编程习惯,但如果响应已经提交,有没有办法处理异常?
"响应已经提交"和"标题已经发送给客户"的含义究竟是什么?
在我的 servlet 我有这个代码:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Utils.CheckSession(request,response);
String op = request.getParameter("op");
int DaysToAdd = request.getParameter("daysToAdd") != null ? Integer.valueOf(request.getParameter("daysToAdd")) : 0;
ArrayList<Giorno> giorni = new ArrayList<Giorno>();
/*HERE I FILL ArrayList giorni and calculate other variables*/
if ("cal".equals(op))
{
request.setAttribute("giorni", giorni);
request.setAttribute("daysToAdd", DaysToAdd);
request.getRequestDispatcher("GestioneCalendario.jsp").forward(request, response);
}
else if("utente".equals(op))
{
// ricavare abbonamento dell'utente
String idu = (String) request.getAttribute("idu");
Abbonamento a = null;
int iDa = Utils.getIdAbbonamentoAttivoUtente(idu);
a = Utils.getAbbonamentoFromId(iDa); …Run Code Online (Sandbox Code Playgroud) 我想实现这一点。
在我的page1.jsp中,我有一个链接,该链接触发了一个servlet。
<a href="Servlet">link1</a>
Run Code Online (Sandbox Code Playgroud)
因此,当用户按下link1时,除其他外,该servlet我想做两件事。
1将对象传递到下一页page2.jsp,
2将用户重定向到page2.jsp
在Servlet中,我写道:
request.setAttribute("cis", myObjet);
RequestDispatcher disp = getServletContext().getRequestDispatcher("/page2.jsp");
disp.forward(request, response);
response.sendRedirect("page2.jsp");
Run Code Online (Sandbox Code Playgroud)
运行该应用程序后,我在控制台中看到一条错误消息:
提交响应后无法调用sendRedirect()
我读了一些与此有关的主题,但我还没有解决。该应用程序也继续工作,尽管我在控制台中有此错误...非常感谢!