Sam
Sam

Reputation: 4633

JSP one page passes value to the other page

I do this with jetty web server.

The precondition is the web server has been launched.

My main page written in JSP is trying to get the ip address from the incoming connection.

Therefore, I separate it to two JSP pages.(Let's say, a.jsp and b.jsp)

b.jsp is for getting the client ip address and then passes it to the a.jsp while

a.jsp is purposely designed for displaying the ip address obtaining from b.jsp.

I have a static String in my b.jsp for storing the incoming connection ip address.

Two questions:

  1. I hope a.jsp will display nothing unless it gets the data(ip address) sent from b.jsp. How can I update the page?

  2. How to pass the value from the b.jsp to a.jsp?

Thanks in advance

my b.jsp looks like...

<%@ include file="index.jsp" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org  /TR/html4/loose.dtd">
    <%!
    static String clientIpAddress = "";
    %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <% 
        clientIpAddress=request.getRemoteAddr();
        if(clientIpAddress!=null){
            //How can I pass clientIpAddress to a.jsp to display?
        }
        else{
            out.println("Nothing happened here");
        }
    %>




</body>
</html>

I added

<%@ include file="a.jsp" %>

at the top of my code since I think I have to do it for passing the clientIpAddress to a.jsp.

  1. Will a.jsp updates itself automatically when it receives the data from b.jsp?

Upvotes: 0

Views: 325

Answers (2)

Nir Alfasi
Nir Alfasi

Reputation: 53525

You don't need to pass the IP address between the pages, you can skip b.jsp and send the user directly to a.jsp - there you can pull the IP address using:
request.getRemoteAddr()

As for your question in regards to passing values between the pages, I would do it either by saving the value to the session, or, add a GET/POST parameter before the redirect.

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94635

You can use <jsp:include/> or <jsp:forward/> actions.

<jsp:forward page="a.jsp">
  <jsp:param name="foo" value="10"/>
</jsp:forward>

Upvotes: 4

Related Questions