<body> <c:if test="${!(empty param.username)&&!(empty param.password) } "> <% String username=request.getParameter("username"); String password=request.getParameter("password"); if(username.equals("wulian2020")&&password.equals("123")){ Cookie cookie_username=new Cookie("username",username); Cookie cookie_password=new Cookie("password",password); cookie_username.setMaxAge(60); cookie_password.setMaxAge(60); response.addCookie(cookie_username); response.addCookie(cookie_password); response.sendRedirect("index.jsp"); } else { response.setContentType("text/html;charset=utf-8"); out.write("账号或密码错误!"); } %> </c:if> <form action="login.jsp" method="post"> 账号:<input type="text" name="username"/><br> 密码:<input type="password" name="password"/><br> <input type="submit" value="登录" /> </form> </body>运行时,密码错误不输出错误信息
时间: 2023-12-22 07:05:16 浏览: 35
是的,这段代码在密码错误时没有输出错误信息,这可能会给用户带来一些困惑。建议在 else 分支中加入一个提示信息,比如:
```java
else {
response.setContentType("text/html;charset=utf-8");
out.write("账号或密码错误,请重新输入!");
}
```
这样即使密码错误,用户也能够得到相应的提示信息,更加友好和人性化。
相关问题
<body> <c:if test="${!(empty param.username)&&!(empty param.password) } "> <% String username=request.getParameter("username"); String password=request.getParameter("password"); if(username.equals("wulian2020")&&password.equals("123")){ Cookie cookie_username=new Cookie("username",username); Cookie cookie_password=new Cookie("password",password); cookie_username.setMaxAge(60); cookie_password.setMaxAge(60); response.addCookie(cookie_username); response.addCookie(cookie_password); response.sendRedirect("index.jsp"); } else { response.setContentType("text/html;charset=utf-8"); out.write("账号或密码错误!"); } %> </c:if> <form action="login.jsp" method="post"> 账号:<input type="text" name="username"/><br> 密码:<input type="password" name="password"/><br> <input type="submit" value="登录" /> </form> </body>
这是一个简单的 JSP 页面,包括一个表单和一个条件语句。当用户提交表单时,会判断用户名和密码是否正确,如果正确则创建两个 Cookie 并重定向到 index.jsp 页面,如果不正确则输出错误信息。需要注意的是,这段代码存在一定的安全隐患,比如没有对参数进行输入合法性检查,可能会导致 SQL 注入等攻击。建议在实际应用中对代码进行进一步的优化和加固。
利用EL和JSTL技术,改进用户登录页面(login.jsp),当用户信息验证成功则转到主页面(main,jsp)显示问候语,如“用户liming,你好!”,否则转回login.jsp,保留原来用户输入的用户名,并有提示信息:“用户名或密码错误,请重新输入”。实验代码
以下是一个使用 EL 和 JSTL 实现用户登录验证和跳转的示例代码:
login.jsp:
```jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<c:if test="${not empty param.username and not empty param.password}">
<c:set var="username" value="${param.username}" />
<c:set var="password" value="${param.password}" />
<c:choose>
<c:when test="${username eq 'admin' and password eq 'password'}">
<c:set var="loggedIn" value="true" />
</c:when>
<c:otherwise>
<c:set var="loggedIn" value="false" />
<c:set var="errorMessage" value="用户名或密码错误,请重新输入。" />
</c:otherwise>
</c:choose>
</c:if>
<c:if test="${loggedIn == true}">
<c:url value="main.jsp" var="mainUrl" />
<c:redirect url="${mainUrl}">
<c:param name="username" value="${username}" />
</c:redirect>
</c:if>
<c:if test="${loggedIn != true}">
<form method="post" action="login.jsp">
<p>${errorMessage}</p>
<label>Username:</label>
<input type="text" name="username" value="${param.username}" /><br />
<label>Password:</label>
<input type="password" name="password" /><br />
<input type="submit" value="Login" />
</form>
</c:if>
</body>
</html>
```
main.jsp:
```jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Main</title>
</head>
<body>
<h1>Main</h1>
<p>用户${param.username},你好!</p>
</body>
</html>
```
在这个例子中,我们首先检查是否有传递 username 和 password 参数。如果有,我们将它们存储为变量。然后,使用 `<c:choose>` 标签来判断用户名和密码是否匹配。如果匹配,我们将 loggedIn 变量设置为 true,否则设置为 false,并设置一个错误消息。接下来,我们使用 `<c:if>` 标签来检查 loggedIn 的值。如果为 true,我们使用 `<c:redirect>` 标签将用户重定向到 main.jsp 页面,并传递用户名作为参数。否则,我们显示登录表单,保留原来输入的用户名,并显示错误消息。
请注意,在 main.jsp 页面中,我们使用 `<c:param>` 标签将用户名作为参数传递。这样,我们就可以在 main.jsp 页面中显示问候语了。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)