在JSP中使用POST方法传递数据时,如何取得数据来源的服务器地址(非客户地址)???
本文导语: | 把下面的servlet编译后自己试试看吧。http://localhost/servlet/Info import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class Info extends HttpServlet { ServletConfig config; public void init(S...
|
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Info extends HttpServlet {
ServletConfig config;
public void init(ServletConfig tconfig) throws ServletException {
super.init(tconfig);
config=tconfig;}
public String getServletInfo() {
return "Info version 1.9: a demo and diagnostic servlet from vqSoft";}
private void pp(PrintWriter os, String name, String value) {
os.print(""+name+": ");
if (value==null)
os.print("none");
else
os.print(value);
os.println("
");}
private void pp(PrintWriter os, String name, int value) {
pp(os, name, String.valueOf(value));}
public void dostuff(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
log("invoked");
rep.setContentType("text/html");
PrintWriter os=rep.getWriter();
os.println("");
os.println("Info servlet");
os.println("");
os.println("");
os.println("");
os.println("Info servlet");
os.println("");
os.println("
This page was generated by Info version 1.9, a demo and diagnostic servlet supplied with vqServer. ");
os.println("Info lists its initialisation parameters, information about the http request which invoked it and the http request parameters passed to it.
os.println("");
os.println("Servlet initialisation parameters");
Enumeration e=config.getInitParameterNames();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
String name=(String) e.nextElement();
String value=config.getInitParameter(name);
pp(os, name, value);}
os.println("");
os.println("Request parameters");
pp(os, "Request method", req.getMethod());
pp(os, "Request URI", req.getRequestURI());
pp(os, "Request protocol", req.getProtocol());
pp(os, "Servlet path", req.getServletPath());
pp(os, "Path info", req.getPathInfo());
pp(os, "Path translated", req.getPathTranslated());
pp(os, "Query string", req.getQueryString());
pp(os, "Content length", req.getContentLength());
pp(os, "Content type", req.getContentType());
pp(os, "Server name", req.getServerName());
pp(os, "Server port", req.getServerPort());
pp(os, "Remote user", req.getRemoteUser());
pp(os, "Remote address", req.getRemoteAddr());
pp(os, "Remote host", req.getRemoteHost());
pp(os, "Authorization scheme", req.getAuthType());
os.println("");
os.println("Request headers");
e=req.getHeaderNames();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
String name=(String) e.nextElement();
String value=(String) req.getHeader(name);
pp(os, name, value);}
os.println("");
os.println("Servlet parameters");
e=req.getParameterNames();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
String name=(String) e.nextElement();
String value=req.getParameter(name);
StringBuffer tbuffer=new StringBuffer();
pp(os, name, value);}
os.println("");
os.println("Other servlets (by name)");
ServletContext tcontext=config.getServletContext();
e=tcontext.getServletNames();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
os.println((String) e.nextElement()+"
");}
os.println("");
os.println("Other servlets");
e=tcontext.getServlets();
if (!e.hasMoreElements())
os.println("None!");
else while (e.hasMoreElements()) {
Servlet tservlet=(Servlet) e.nextElement();
os.println(tservlet.getServletInfo()+"
");}
os.println("");
os.println("
Info version 1.9. Info and vqServer are copyright © 1997-99 Steve Shering and vqSoft.");
os.println("");
os.println("");
os.println("");
os.println("Home page");
os.println("");
os.println("");
os.println("");
os.flush();}
public void doGet(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
dostuff(req, rep);}
public void doPost(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
dostuff(req, rep);}}
|
1。在你的form提交页设一个session,在form的执行页验证这个session,通过才处理。
2。执行页检查是那一个页面提交来的,好象有一个request.getReferr()方法能得到用户从那一页来到本页,方法名记不大清了,手边没有资料,你查一下文档吧,应该有。从head信息也能够得到,resin,tomacat文档有。
|
|
|
|
request.getServerPort()
|
这个不就是POST的叶面地址。
|
|
返回当前服务器的地址