本文共 2819 字,大约阅读时间需要 9 分钟。
路径一共分为相对路径、绝对路径和根路径。
用在同一个网站中, aaa/1.jpg,仅限静态资源,如果页面比较多,并且使用框架,会出现混乱。
用在不同网站之间的跳转,比如:http://www.baidu.com/aaa/1.jpg
http://localhost:8080/day12web1/loginservlet
(1)如果是作用于服务器,根指定就是主机名(服务器) 【/】 表示 (/day12web1)
(2)如果是作用于浏览器,就是在页面中写的路径,【/】标示(http://localhost:8080),要写服务器名重定向就是通过各种方法将网络请求重新定个方向转到其它位置。
@WebServlet(name = "LoginServlet", value = "/loginservlet")public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //重定向原理就是通过响应头Headers下的Location实现的 String username = request.getParameter("username"); String password = request.getParameter("password"); if (username.equals("admin") && password.equals("123456")) { //登录成功,重定向,跳转首页 //这个方法是给浏览器使用,根目录必须有服务器名 response.sendRedirect("/0831web1/index.html"); } else { //登录失败 response.getWriter().println("用户名或密码错误"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }}
@WebServlet(name = "RegistServlet", value = "/registservlet")public class RegistServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //可以将转发代码的response.setContentType消除 //或者将login.html改为jsp //response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String repassword = request.getParameter("repassword"); String email = request.getParameter("email"); System.out.println("用户名" + username + ",密码" + password + ",邮箱:" + email); System.out.println("注册成功"); //获取转发器,然后forwoard执行转发,这里地址只能使用相对地址和根地址,只能转发本网站 //该方法是在服务器中使用,所以/就代表了服务器名 request.getRequestDispatcher("/login.html").forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }}
转载地址:http://asgzi.baihongyu.com/