JSP中有如下form登陆表单,其中应该采用何种方式进行提交? Get Post Put
时间: 2024-01-19 10:03:43 浏览: 168
在这种情况下,应该采用POST方式提交表单数据。
因为表单中有用户名和密码等敏感信息,使用GET方式提交会将这些信息明文显示在URL中,存在安全风险。而使用PUT方式提交需要指定资源的URI,这通常用于更新或创建资源,不适合用于表单提交。
相比之下,POST方式提交表单数据更加安全,因为它将表单数据放在HTTP请求的正文中,不会在URL中暴露敏感信息。同时,POST方式也更加灵活,可以提交大量数据,支持文件上传等功能。
因此,建议采用POST方式提交表单数据,如下所示:
```html
<form method="post" action="login.jsp">
<label>Username:</label>
<input type="text" name="username" />
<br />
<label>Password:</label>
<input type="password" name="password" />
<br />
<input type="submit" value="Login" />
</form>
```
相关问题
在Servlet中完成Web登录验证程序的编写: 在前端login.jsp页面完成用户名和密码的输入,将表单提交到LoginServlet.java中进行验证。调用UserManager提供的方法,用于验证用户名和密码是否正确,如正确,则将用户名保存在session中,并跳转到欢迎页面,该页面应能从session中得到用户名;如不正确,则跳转到登录页面。
好的,下面是代码实现:
1. 在login.jsp页面中,添加表单并设置action为LoginServlet的url:
```html
<form method="post" action="LoginServlet">
<label>用户名:</label>
<input type="text" name="username"><br>
<label>密码:</label>
<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
```
2. 创建LoginServlet.java,并在doPost方法中获取登录用户名和密码,调用UserManager提供的方法进行验证:
```java
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
UserManager userManager = new UserManager();
boolean isValidUser = userManager.isValidUser(username, password);
if (isValidUser) {
request.getSession().setAttribute("username", username);
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
}
```
3. 在UserManager类中提供isValidUser方法,用于验证用户名和密码是否正确:
```java
public class UserManager {
private Map<String, String> users = new HashMap<>();
public UserManager() {
users.put("admin", "123456");
users.put("user", "password");
}
public boolean isValidUser(String username, String password) {
String savedPassword = users.get(username);
return savedPassword != null && savedPassword.equals(password);
}
}
```
4. 在welcome.jsp页面中,获取保存在session中的用户名并显示欢迎信息:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>欢迎 <%= session.getAttribute("username") %>!</h1>
</body>
</html>
```
这样就完成了一个基本的Web登录验证程序。
编写一个jsp页面,要求用户输入自己的姓名.性别,班级和爱好,提交后在第二个页面进行处理,将填写的信息进行显示
在Java Servlets和JSP中创建这样的动态网页流程通常是这样的:
首先,我们需要在前端(JSP页面)创建一个表单让用户输入信息:
```html
<!-- index.jsp -->
<!DOCTYPE html>
<html>
<head>
<title>个人信息录入</title>
</head>
<body>
<form action="submit_info.jsp" method="post">
<label for="name">姓名:</label>
<input type="text" id="name" name="name"><br><br>
<label for="gender">性别:</label>
<select id="gender" name="gender">
<option value="male">男</option>
<option value="female">女</option>
</select><br><br>
<label for="class">班级:</label>
<input type="text" id="class" name="class"><br><br>
<label for="hobby">爱好:</label>
<textarea id="hobby" name="hobby"></textarea><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
在这个例子中,表单的`action`属性指向了另一个JSP文件`submit_info.jsp`,用户点击提交后,表单数据会发送到这个后端处理页面。
然后,在`submit_info.jsp`中,我们接收到这些数据并进行处理:
```jsp
<!-- submit_info.jsp -->
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>个人信息显示</title>
</head>
<body>
<%@ page import="java.util.*" %>
<%!
Map<String, String> userInfo = new HashMap<>();
%>
<%-- 这里假设request.getParameterMap()已经获取到了用户的输入 --%>
<%
Map<String, String[]> params = request.getParameterMap();
for (String key : params.keySet()) {
String[] values = params.get(key);
if (values.length > 0) {
userInfo.put(key, values[0]);
}
}
%>
<h2>用户信息:</h2>
<p>姓名: <%= userInfo.get("name") %></p>
<p>性别: <%= userInfo.get("gender") %></p>
<p>班级: <%= userInfo.get("class") %></p>
<p>爱好: <%= userInfo.get("hobby") %></p>
</body>
</html>
```
这个JSP页面通过`request.getParameterMap()`获取表单提交的数据,并存储在一个`HashMap`中,然后显示出来。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)