JSP页面转发

时间:2015-02-05 09:21:49

标签: java jsp java-ee

我有两个名为" signin.jsp"和" index.jsp"

我从" signin.jsp"上的数据库页面获取用户信息。然后我在" index.jsp"上显示用户信息页面。但网址相同" www.localhost.com:8080 / app / signin.jsp "。我如何以这种方式更改网址" www.localhost.com:8080 / app / index.jsp "当我通过index.jsp。

我因为我的英语不好而感到非常伤心。

的signin.jsp

<%@page import="com.application.manager.UserManager"%>
<%@page import="com.application.entity.User"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%
    String message = "";
    String login = request.getParameter("login");
    String userName = request.getParameter("userName");
    String userPassword = request.getParameter("userPassword");

    if (login != null) {

        if (!userName.trim().isEmpty()
                && !userPassword.trim().isEmpty()) {

            UserManager userManager = new UserManager();
            User user = userManager.getUserInfo(userName, userPassword);

            request.setAttribute("user", user);

            if (user != null) {
                RequestDispatcher requestDispatcher = request
                        .getRequestDispatcher("index.jsp");
                requestDispatcher.forward(request, response);
            } else {
                RequestDispatcher requestDispatcher = request
                        .getRequestDispatcher("signin-error.jsp");
                requestDispatcher.forward(request, response);
                message = "Kullanici adi veya sifre yanlis.";
            }
        } else {
            message = "Kullanici adi veya sifresi bos olmamalidir.";
        }
    }
%>
<!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=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<title>Signin</title>
</head>
<body>
    <div class="container">
        <div class="jumbotron">
            <h2>Kullanici Giris</h2>
            <p><%=message%></p>
            <form action="signin.jsp" method="post" role="form">
                <div class="form-group">
                    <label for="userName">Kullanici Adi</label> <input type="text"
                        class="form-control" name="userName"
                        placeholer="kullanici adi seciniz...">
                    <div class="form-group">
                        <label for="userName">Sifre</label> <input type="password"
                            class="form-control" name="userPassword"
                            placeholer="kullanici adi seciniz...">
                    </div>
                    <input type="submit" class="btn btn-success" name="login"
                        value="Giris">
            </form>
        </div>
    </div>
</body>
</html>

的index.jsp

<%@page import="com.application.manager.UserManager"%>
<%@page import="com.application.entity.User"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%
    User user = (User) request.getAttribute("user");
    String userName = user.getUserName();
    long userId = user.getUserId();
    String userPassword = user.getUserPassword();
    String userFirstName = user.getUserFirstName();
    String userLastName = user.getUserLastName();
    String userEmail = user.getUserEmail();

    request.setAttribute("user", user);
    String messages = request.getParameter("messages");
    if (messages != null) {
        RequestDispatcher requestDispatcher = request
                .getRequestDispatcher("messages.jsp");
        requestDispatcher.forward(request, response);
    } else {
        System.out.println("Hata");
    }
%>
<!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=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<title>Home</title>
</head>
<body>
    <div class="container">
        <div class="jumbotron">
            <p>Giris basarili!</p>
            <p><%=userId%></p>
            <p><%=userName%></p>
            <p><%=userFirstName%></p>
            <p><%=userLastName%></p>
            <p><%=userEmail%></p>
            </form>
        </div>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:4)

相同网址的原因是,

 RequestDispatcher requestDispatcher = request
                        .getRequestDispatcher("index.jsp");
 requestDispatcher.forward(request,response);

forwards请求,因此网址保持不变。您可以使用sendRedirect()方法,每次都会触发新请求。

但请记住,您无法在请求中设置任何属性,因为每次都会触发新HttpServletRequest

见: