[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

Get Client IP info from a Servlet into a Class

Asked by jandersonwidener in Java Server Pages (JSP), TCP/IP, Java Servlets

Tags: java, InetAddress, Host IP, Client Request, Server Response, Servlet, Class, beginner java

I can successfully get the Host IPAddress of the requesting client to return to the screen fine in (File 1 my.jsp) and on all .jps page(s) via my serializable servlet session thanks to File 2 ViewDefinition Class.

But when I try to Add the IP information to send that info to an email notification not just return it to the JSP Screen I get null or it blows up the page no matter how I try it using File 3 MailNotification Class, File 4 Email Notifications I stream in vis File 5 webconfig which assigns the email address as a global variable and my File 6 which Processes the Email to be sent.

I know what I need to do -- also carry the getSessionIPAddress into other classes
but cannot seem to figure out how can I extend the instance life of the servlet variable  this.sessionIPAddress and assign it to a variable to email ?

Please help.

Thanks sooo much !

Should I add the String to my Default constructor or something else ?
When I try to make getSessionIPAddress static to carry the IP, the IP is returned but then the rest of my surrounding code blows out...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
------------------------------------------ File 1 ----------------------------------------------------
<!-- /WEB-INF/jsp/my.jsp -->
<jsp:useBean id="viewDefinition" scope="request" class="mytld.mycompany.projectpackage.MVC_view.ViewDefinition" />
<jsp:getProperty name="viewDefinition" property="sessionIPAddress"/>
<!-- this works fine -->
 
------------------------------------------ File 2 ----------------------------------------------------
/** ViewDefiition **/
package mytld.mycompany.projectpackage.MVC_view;
 
public class ViewDefinition implements java.io.Serializable {
  public String sessionIPAddress;
 
 /**   * Default Constructor   **/
  public ViewDefinition() {
  }
 
// and accessor method ::.
  public String getSessionIPAddress() {
	  return sessionIPAddress;
  }
 
// and mutator method ::.
  public void setSessionIPAddress(String sessionIPAddress) {
	  this.sessionIPAddress = sessionIPAddress;
  }
} // end class
 
------------------------------------------ File 3 ----------------------------------------------------
/** Mail notification **/
package mytld.mycompany.projectpackage.MVC_model;
 
 
public class MailNotification implements Serializable  {
private static final String IP_PATTERN =  "${userhostip}";
 
 /**   * Default Constructor   **/
public MailNotification() {
}
 
public String getMessage() {
 
// I know this is WRONG -- it works but blow out other code... Any ideas ?
ViewDefinition ipview = new ViewDefinition();		
String reapIP = ipview.getSessionIPAddress();
 
String text = message;
text = replace(text, IP_PATTERN, reapIP);
return text;
}
 
} // end class
------------------------------------------ File 4  ----------------------------------------------------
<!-- /WEB-INF/notifications.xml -->
 
<notification notificationType="EMAIL_IP">
<subject>IP Alert</subject>
<message><![CDATA[
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>IP Notice</title>
</head><body>
    <p style="${style}">User's IP: <strong>${userhostip}</strong></p>
</body></html>
]]></message>
<mime_type>text/html</mime_type>
</notification>
------------------------------------------ File 5  ----------------------------------------------------
 
<!--  /WEB-INF/webconfig.xml -->
 
<servlet>
<servlet-name>controlservlet</servlet-name>
<servlet-class>mytld.mycompany.projectpackage.controller.ControlServlet</servlet-class>
<init-param>
<description>Email Destination</description>
<param-name>infosecEmail</param-name>
<param-value>myTargetEmail@myDomainName.tld</param-value>
</init-param>
</servlet>
 
------------------------------------------ File 6  ----------------------------------------------------
/** Processor Class **/
package mytld.mycompany.projectpackage.controller;
 
import java.io.IOException;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
import mytld.mycompany.projectpackage.MVC_model.*;
import mytld.mycompany.projectpackage.MVC_view.*;
 
try {
MailNotification notification = MailNotification.loadNotification(ctx.getResourceAsStream("/WEB-INF/notifications.xml"), NotificationType.EMAIL_IP);
Util.sendMail(Util.INFOSEC_EMAIL, Util.DEFAULT_FROM, notification.getSubject(), notification.getMessage(resetAccount), notification.getMimeType());
catch (Exception mex) {
mex.printStackTrace();
logger.log(Logger.ERROR, "IP Error +mex.getMessage(), request.getRemoteAddr()); // it works here ? Why cant I pass the HostInetAddress to the Email Message Object ? AUURRGGGHH ! ;)
}							
return viewDefinition;
}
break;
[+][-]10/19/09 06:36 AM, ID: 25604995Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Java Server Pages (JSP), TCP/IP, Java Servlets
Tags: java, InetAddress, Host IP, Client Request, Server Response, Servlet, Class, beginner java
Sign Up Now!
Solution Provided By: jandersonwidener
Participating Experts: 1
Solution Grade: A
 
[+][-]10/13/09 05:22 PM, ID: 25566151Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/19/09 08:51 AM, ID: 25606336Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20080625