1

My website is working fine in all browsers for Windows and Android devices. But java sessions are returning null values on JSP pages when it comes to iOS devices for all browsers.

testinput.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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">
<title>Insert title here</title>
</head>
<body>
<form action="testing" method="post">
    <input type="text" name="pdf1">
    <input type="text" name="pdf2">
    <button type="submit">click me</button>
</form>
</body>
</html>

testing.java (servlet):

import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession;

/**  * Servlet implementation class testing  */ @WebServlet("/testing") 

public class testing extends HttpServlet {  
private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public testing() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**      
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)   
*/  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
// TODO Auto-generated method stub      
response.getWriter().append("Served at: ").append(request.getContextPath());    }


/**      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */     
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      
String abc = request.getParameter("pdf1").toString();       
String def = request.getParameter("pdf2").toString();

HttpSession sess = request.getSession();        
sess.setAttribute("pdf1", abc);         
sess.setAttribute("pdf2", def);         
if(sess.getAttribute("pdf1")!=null){            
Cookie cook = new Cookie("login", sess.getAttribute("pdf1").toString());             

response.addCookie(cook);       
}       
response.sendRedirect("test.jsp");      
return;     
}
}

test.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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">
<title>Insert title here</title>
</head>
<body>
    <%try{ %>
    <% Cookie cook[] = request.getCookies();

    for(Cookie c :cook){
        if(c.getName().equals("pdf1")){
            String str = c.getValue();%>
            <p><%=str%></p> 
        <%}
    }%>     
    <p><%=session.getAttribute("pdf1").toString() %></p>
    <p><%=session.getAttribute("pdf2").toString() %></p>
    <%}catch (Exception e){ 
        e.printStackTrace();
        log("erroristhis"+e);
        }%>
</body>
</html>

Apache:

I added below lines to my https.conf. virtualHost*:80 files in my linux vps server to access session variables.

<VirtualHost *:80>
ServerName example.com
ServerAlias mail.example.com www.example.com
#  DocumentRoot /home/example1/public_html
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
JkMount  /* example
<Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>
  ProxyRequests Off
  ProxyPreserveHost On
  ProxyPass               /       http://localhost:8080/example/
  ProxyPassReverse        /       http://localhost:8080/example/
  ProxyPassReverseCookiePath        /   domine/
</VirtualHost>
jww
  • 97,681
  • 90
  • 411
  • 885
Ravi varma
  • 11
  • 3
  • why don't you debug your `doPost` method and see what is happening – Scary Wombat May 28 '18 at 01:37
  • i did session.getAttribute("pdf1") returning null value on jsp pages its working fine on servlet. – Ravi varma May 28 '18 at 02:20
  • check https://stackoverflow.com/questions/2138245/session-is-lost-and-created-as-new-in-every-servlet-request – Scary Wombat May 28 '18 at 02:22
  • hi #Scary Wombat i appreciate that you are helping we with this. my sessions are working fine on my local tomcat server. and on all windows and android application browsers. the issue is when it comes to IOS devises the sessions are returning null values that too only on jsp pages they are working fine on servlets too. i am confused! i was thinking maybe i didnt configured httpd.conf fine on my linux VPS server. correct me if i am wrong. Thank you. – Ravi varma May 29 '18 at 00:29

0 Answers0