用户名:
密码:
赞
踩
webapp代码
开发工具:IntelliJ IDEA
登录代码(login.jsp)
<body>
<form action="do.jsp" method="get">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
判断跳转代码(do.jsp)
<body>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
if (username.equals("丽丽")&&password.equals("123")){
session.setAttribute("username",username);
response.sendRedirect("success.jsp");
}else {
response.sendRedirect("login.jsp");
}
%>
</body>
成功代码(success.jsp)
<body>
<%
String username=(String)session.getAttribute("username");
if (username!=null){
out.print(username+"欢迎您");
}else {
out.print("<script>alert(‘您还没有登录请先登录’);</script>");
out.print("<script>location.href='login.jsp';</script>");
}
%>
</body>
pom.xml依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
页面展示
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。