JSP中读取远程机器的PROPERTIES文件和IP地址
类别: JSP教程
This servlet could be used for basic security in servlet.
When you want to limit the computers that can call a specific servlet.
You read the valid IP address from a properties file.
The IP of the computer calling the servlet (static IP) is compared with value in the property file.
If the values are the same then it is called from the correct computer.
Otherwise a message is displayed showing the error.
For demonstration purposes the IP Addresses are displayed.
Save the file as GetIPAddressServlet.java
Create text file called readip.props
Inside the file type:
ip=xxx.xxx.xx.xx
replace the ´x´ with the actual IP address you want to store. This will be used to compare with the IP address of the computer calling the servlet.
Copy the readip.props file into a directory which the servlet will be able to find. For example, c:winntsystem32
Compile the servlet.
javac GetIPAddressServlet.java
Copy the servlet class file into the servlet directory on the webserver (read vendor´s documentation).
Call the servlet http://localhost/GetIPAddressServlet
and it should display the IP addresses and the results of the comparison.
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetIPAddressServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<HTML><HEAD><TITLE>");
out.println("Get IP Address Servlet");
out.println("</TITLE></HEAD>");
out.println("<BODY>");
// Read properties file.
Properties properties = new Properties();
try
{
properties.load(new FileInputStream("readip.props"));
}
catch(IOException e)
{
e.printStackTrace();
}
String ipAddress = "";
String remoteIPAddress = "";
// Read the value of key - ip
ipAddress = properties.getProperty("ip");
out.println("ip address (from properties file) =" + ipAddress);
out.println("
");
// read the (remote) IP address of the requesting computer
remoteIPAddress = request.getRemoteAddr();
request.
out.println("remote ip address =" + remoteIPAddress);
out.println("
");
if (ipAddress.equals(remoteIPAddress))
{
out.println("Same IP Address");
}
else
{
out.println("Sorry, this is not the same IP Address");
}
out.println("</BODY></HTML>");
out.close();
}
}
When you want to limit the computers that can call a specific servlet.
You read the valid IP address from a properties file.
The IP of the computer calling the servlet (static IP) is compared with value in the property file.
If the values are the same then it is called from the correct computer.
Otherwise a message is displayed showing the error.
For demonstration purposes the IP Addresses are displayed.
Save the file as GetIPAddressServlet.java
Create text file called readip.props
Inside the file type:
ip=xxx.xxx.xx.xx
replace the ´x´ with the actual IP address you want to store. This will be used to compare with the IP address of the computer calling the servlet.
Copy the readip.props file into a directory which the servlet will be able to find. For example, c:winntsystem32
Compile the servlet.
javac GetIPAddressServlet.java
Copy the servlet class file into the servlet directory on the webserver (read vendor´s documentation).
Call the servlet http://localhost/GetIPAddressServlet
and it should display the IP addresses and the results of the comparison.
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetIPAddressServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<HTML><HEAD><TITLE>");
out.println("Get IP Address Servlet");
out.println("</TITLE></HEAD>");
out.println("<BODY>");
// Read properties file.
Properties properties = new Properties();
try
{
properties.load(new FileInputStream("readip.props"));
}
catch(IOException e)
{
e.printStackTrace();
}
String ipAddress = "";
String remoteIPAddress = "";
// Read the value of key - ip
ipAddress = properties.getProperty("ip");
out.println("ip address (from properties file) =" + ipAddress);
out.println("
");
// read the (remote) IP address of the requesting computer
remoteIPAddress = request.getRemoteAddr();
request.
out.println("remote ip address =" + remoteIPAddress);
out.println("
");
if (ipAddress.equals(remoteIPAddress))
{
out.println("Same IP Address");
}
else
{
out.println("Sorry, this is not the same IP Address");
}
out.println("</BODY></HTML>");
out.close();
}
}
-= 资 源 教 程 =-
文 章 搜 索