• 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>

  • 相关阅读:
    咏南树形下拉列表数据敏感控件--TYNdbTreeList
    unigui session超时时间设置
    datasnap——动态注册服务类
    DataSnap——利用TParams进行多表事务更新
    咏南新CS插件开发框架支持DELPHI7
    centos7 samba安装与配置
    centos7设置系统语言为中文
    yndbtree控件
    Linux ClientDataSet libmidas.so.2
    菜单树
  • 原文地址:https://www.cnblogs.com/yh-simon/p/12236666.html
Copyright © 2020-2023  润新知