测试获得url的地址
1.编写一个TestGetUrl的测试类,代码如下:
package eduask;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//测试获得url的地址;
public class TestGetUrl extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String url=req.getRequestURI();
System.out.println(url);
resp.getWriter().write(url);
}
}
2.在web.xml文件中配置相关属性,配置结果如下为:
<!--测试获得url-->
<servlet>
<servlet-name>url</servlet-name>
<servlet-class>eduask.TestGetUrl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>url</servlet-name>
<url-pattern>/url</url-pattern>
</servlet-mapping>
3.启动tomcat,之后在浏览器中输入http://localhost:8080/webDemo/url
4.浏览器中会显示:/webDemo/url
5.控制台会显示:/webDemo/url