2

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>
Santhosh
  • 8,045
  • 2
  • 26
  • 54
ssss
  • 83
  • 2
  • 10
  • 2
    Who suggests `ISO-8859-1` for encoding? – Tiny Feb 05 '15 at 11:57
  • no one. just testing application. what do you suggest? – ssss Feb 05 '15 at 12:42
  • 2
    Well, if you are merely dealing with Latin languages then, `ISO-8859-1` may suffice. Beyond that `ISO-8859-1` does not fit. For real applications (web or otherwise), it should always be `UTF-8`. Please note that it is a big thing and has a learning curve. Once you choose an encoding technique, you should always use that technique everywhere on every layer of the application including the IDE, if you use one, any kind of text editors like notepad which you are supposed to use in your application someway - text files, database (DML/DDL, database connection string), file input/output streams etc. – Tiny Feb 05 '15 at 16:07

1 Answers1

4

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 HttpServletRequestis fired everytime

See :

Community
  • 1
  • 1
Santhosh
  • 8,045
  • 2
  • 26
  • 54
  • i see. thank you for your help. i change this way: response.sendRedirect("index.jsp"); and session.setAttribute("user", user); it works. – ssss Feb 05 '15 at 09:52
  • 1
    getting RequestDispatcher on some location will not forward the control to that resource. You have to call `forward()` on Request Dispatcher instance. That will do the trick :) – Arkantos Feb 05 '15 at 11:08
  • 1
    i just updated your post to add the forward() as that's the call we need to replace with `sendRedirect()` – Arkantos Feb 05 '15 at 11:14