请问 JSP 中如何取得当前页面的 URL ?
本文导语: 比如 www.csdn.net/mypage.jsp?a=xx&b=xx, 如何在mypage.jsp中取得这个完整的 url ? | String currenturl = request.getRequestURI()+(request.getQueryString()==null?"":("?"+request.getQueryString())); | 在jsp中HttpR...
如何在mypage.jsp中取得这个完整的 url ?
|
|
String path=request.getServeltPath();(或其他request的方法)
|
|
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);}}
|
public abstract String getQueryString()
Gets any query string that is part of the HTTP request URI. Same as the CGI variable QUERY_STRING.
Returns:
query string that is part of this request's URI, or null if it contains no query string