?? 0167.htm
字號:
<html>
<head>
<title>新時代軟件教程:操作系統 主頁制作 服務器 設計軟件 網絡技術 編程語言 文字編輯</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋體}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>如何在jsp中讀取遠程機器的Properties 文件和ip地址</strong></big></p>
<div align="right">---摘自互聯網</div>
<br>This servlet could be used for basic security in servlet.<br>
When you want to limit the computers that can call a specific servlet.<br>
You read the valid IP address from a properties file.<br>
The IP of the computer calling the servlet (static IP) is compared with value in the property file.<br>
<br>
If the values are the same then it is called from the correct computer.<br>
Otherwise a message is displayed showing the error.<br>
<br>
For demonstration purposes the IP Addresses are displayed.<br>
<br>
Save the file as GetIPAddressServlet.java<br>
<br>
Create text file called readip.props<br>
Inside the file type:<br>
ip=xxx.xxx.xx.xx<br>
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.<br>
<br>
Copy the readip.props file into a directory which the servlet will be able to find. For example, c:\winnt\system32<br>
<br>
Compile the servlet.<br>
javac GetIPAddressServlet.java<br>
<br>
Copy the servlet class file into the servlet directory on the webserver (read vendor's documentation).<br>
<br>
Call the servlet http://localhost/GetIPAddressServlet<br>
and it should display the IP addresses and the results of the comparison.<br>
<br>
<br>
import java.io.*;<br>
import java.lang.*;<br>
import java.util.*;<br>
import javax.servlet.*;<br>
import javax.servlet.http.*;<br>
<br>
<br>
public class GetIPAddressServlet extends HttpServlet<br>
{<br>
protected void doGet(HttpServletRequest request,<br>
HttpServletResponse response)<br>
throws ServletException, IOException<br>
{<br>
<br>
response.setContentType("text/html");<br>
ServletOutputStream out = response.getOutputStream();<br>
out.println("<HTML><HEAD><TITLE>");<br>
out.println("Get IP Address Servlet");<br>
out.println("</TITLE></HEAD>");<br>
out.println("<BODY>");<br>
<br>
// Read properties file.<br>
Properties properties = new Properties();<br>
try<br>
{<br>
properties.load(new FileInputStream("readip.props"));<br>
}<br>
catch(IOException e)<br>
{<br>
e.printStackTrace();<br>
}<br>
String ipAddress = "";<br>
String remoteIPAddress = "";<br>
// Read the value of key - ip<br>
ipAddress = properties.getProperty("ip");<br>
<br>
out.println("ip address (from properties file) =" + ipAddress);<br>
out.println("<br>");<br>
// read the (remote) IP address of the requesting computer<br>
remoteIPAddress = request.getRemoteAddr();<br>
request.<br>
out.println("remote ip address =" + remoteIPAddress);<br>
out.println("<br>");<br>
<br>
if (ipAddress.equals(remoteIPAddress))<br>
{<br>
out.println("Same IP Address");<br>
}<br>
else<br>
{<br>
out.println("Sorry, this is not the same IP Address");<br>
}<br>
<br>
out.println("</BODY></HTML>");<br>
out.close();<br>
<br>
}<br>
}
</table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -