简体   繁体   中英

JSP page forward

I have two pages named "signin.jsp" and "index.jsp"

I get the user information form the database page on "signin.jsp". Then I show the user information page on "index.jsp". But url is same "www.localhost.com:8080/app/ signin.jsp ". How can i change url this way "www.localhost.com:8080/app/ index.jsp " when i passed the index.jsp.

I'm so sory for my bad English.

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>

The reason for the same url is,

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

It forwards the request , so the url remains the same .You can instead use the sendRedirect() method , which fires new request each time .

But remember you can't set any attributes in the request as the new HttpServletRequest is fired everytime

See :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM