我正在處理一個項目,該項目需要一個包含另一個使用Servlet處理登錄表單的JSP頁面的JSP頁面。在jsp頁面上使用servlet表單而不重定向到servlet
我遇到的問題是,無論何時嘗試登錄,servlet工作,表單中可能出現的錯誤都會顯示出來,但鏈接會重定向到Servlet並且不會保留在同一個JSP頁面上。
它如何與一個模板看起來例子:JSPpage.jsp
**text outside of included login form**
<included JSP page with the login form>
Email: something
Password: ******
-JSP頁面點擊登錄後
鏈接:MyServlet
-JSP頁面點擊登錄鏈路之前
<login form>
Email: something
Error: invalid email
Password:
基本上突出顯示的文字是o包含表單的utside消失。這是因爲鏈接變成了Servlet。
我想處理來自JSP頁面內部的登錄,因此登錄表單周圍的任何模板都將保持不變,並且不會被刪除(頁面不會重定向)。
我已經遍尋全面地尋找解決這個問題的辦法。 我意識到我的解釋很虛弱&我爲此道歉。
請好好的幫我一下。我不希望使用AJAX或JavaScript或框架來做到這一點,除非它是必須的,並且不能用JSP/Servlet直接完成。
謝謝, Matej。
編輯: 的Servlet:Prijava.java
package servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import baza.Podatki;
public class Prijava extends HttpServlet{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
HttpSession session = request.getSession();
String email=request.getParameter("email");
String geslo=request.getParameter("geslo");
int st=0;
if (email!=null){
try {
if(!Podatki.shranjenEmail(email))
request.setAttribute("napakaPrijava", "Email ne obstaja!");
else if (!Podatki.preveriPrijavoUporabnik(email, geslo)&&!Podatki.preveriPrijavoZaposlen(email, geslo))
request.setAttribute("napakaPrijava", "Nepravilno geslo!");
else{
request.setAttribute("prijava",true);
session.setAttribute("email", email);
}
request.setAttribute("email", email);
} catch (SQLException e) {
e.printStackTrace();
}
}
else if (request.getParameter("odjava")!=null)
session.removeAttribute("email");
System.out.println(session.getAttribute("email"));
System.out.println("Prijava:"+request.getParameter("prijava"));
RequestDispatcher view = getServletContext().getRequestDispatcher("/Prijava.jsp");
view.forward(request, response);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
doPost(request,response);
}
}
Prijava.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Renta Ferari</title>
<link rel="stylesheet" type="text/css" href="obrazci.css">
</head>
<body >
<div style="width:250px;height:110px; border:solid 1px; bordor-radius:15px; border-color:red">
<c:choose>
<c:when test="${!prijava}">
<form method="post">
<table align="center">
<tr><td>E-mail:</td> <td><input type="text" name="email" class="vnos" value="${email}"></td></tr>
<tr><td>Geslo: </td><td><input type="password" name="geslo" class="vnos"><br></td></tr>
<tr><td colspan="4"><c:out value="${napakaPrijava}"></c:out></td></tr>
<tr><td colspan="2"><input type="submit" name="prijava" value="Prijava"></td></tr>
</table>
</form>
</c:when>
<c:otherwise>
<div align="center">
Prijavljeni ste kot: <c:out value="${email}"></c:out>
<form method="post" action="login">
<input type="submit" name="odjava" value="Odjava">
</form>
</div>
</c:otherwise>
</c:choose>
</div>
</body>
</html>
所以基本上我想有一個新的JSP中,上面的代碼與我的模板的頭一起,內容&頁腳。
我嘗試了很多選項和「解決方案」,比如jsp:include,<%@include文件等等。沒有工作,因爲我想讓他們去。每當我點擊登錄提交按鈕時,它會將我重定向到servlet的URL,從而覆蓋包含的頁眉,頁腳內容&。
我不是要求你編碼它,我只是想了解我如何做到這一點,因爲許多網站都這樣做。
您可以顯示:
然後你就可以在你的servlet轉發給本你的servlet和JSF文件中的代碼? – helderdarocha
我已經添加了相應的代碼,我希望我也解釋得很好。 @helderdarocha – MSpan