• JSP与Servlet跳转路径问题


     

    • web.xml中的 /: 代表项目根路径

    http://localhost:8888/Servlet25Project/

    • jsp中的/:  服务器根路径

    http://localhost:8888/

    • ../   表示返回上一层目录

    自定义创建包下的jsp与Servlet之间的跳转案例

    1. 文件目录

          

    2. login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>登录</title>
    </head>
    <body>
       <form  action="../LoginServlet" method="post">
           用户名:<input type="text" name="uname"><br/>
           密码:   <input type="password" name="upwd"><br/>
            <input  type="submit" value="登录">
           </form><br/>
           ${sessionScope.error}<br/>
           
    </body>
    </html>

    2. LoginServlet

    public class LoginServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
       
        public LoginServlet() {
            super();
        }
    
    	
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		request.setCharacterEncoding("UTF-8");
    		String uName=request.getParameter("uname");
    		String uPwd=""+request.getParameter("upwd");
    //		System.out.println(uName+uPwd);
    		if("admin".equals(uName)&&"admin".equals(uPwd)) {
    			request.getSession().setAttribute("name", uName);
    			response.sendRedirect("test6/welcome.jsp");
    		}else {
    			request.getSession().setAttribute("error", "账户或密码错误,请重新输入!");
    			response.sendRedirect("test6/login.jsp");
    		}
    	}
    
    	
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		doGet(request, response);
    	}
    
    }
    

    3. welcome.jsp

    <body>
         登录成功!<br/>
         <%//String name=(String)session.getAttribute("name"); //session.getAttribute("name")%>
         你好,<%=session.getAttribute("name") %>
    </body>

  • 相关阅读:
    使用zipkin2在SpringCloud2.0环境下追踪服务调用情况
    Spring Cloud负载均衡:使用Feign作客户端负载均衡
    Spring Cloud负载均衡:使用zuul作服务器端负载均衡
    Word模板替换
    【转】Eureka集群
    巧用JavaScript语言特性解耦页面间调用(观察者模式)
    MySQL 视图触发器事务存储过程函数
    MySQL py模块的链接Navicat可视化工具
    MySQL 单表查询多表查询
    MySQL 表与表之间建立关系
  • 原文地址:https://www.cnblogs.com/yh-simon/p/12236666.html
Copyright © 2020-2023  润新知