java web servlet编程题及答案,出10道题
时间: 2024-01-14 12:05:12 浏览: 131
1. 编写一个Servlet,将客户端提交的用户名和密码保存到数据库中。
```java
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");
// 将用户名和密码存储到数据库中
response.getWriter().println("登录成功");
}
}
```
2. 编写一个Servlet,查询数据库中的所有用户信息并返回给客户端。
```java
public class UserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 查询数据库中的所有用户信息
response.getWriter().println(users);
}
}
```
3. 编写一个Servlet,接收客户端上传的文件并保存到服务器上。
```java
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Part filePart = request.getPart("file");
String fileName = filePart.getSubmittedFileName();
InputStream fileContent = filePart.getInputStream();
// 将文件保存到服务器上
response.getWriter().println("文件上传成功");
}
}
```
4. 编写一个Servlet,从数据库中查询商品信息并返回给客户端。
```java
public class ProductServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int productId = Integer.parseInt(request.getParameter("productId"));
// 查询数据库中的商品信息
response.getWriter().println(product);
}
}
```
5. 编写一个Servlet,从客户端接收订单信息并保存到数据库中。
```java
public class OrderServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String productName = request.getParameter("productName");
int quantity = Integer.parseInt(request.getParameter("quantity"));
double price = Double.parseDouble(request.getParameter("price"));
// 将订单信息保存到数据库中
response.getWriter().println("订单提交成功");
}
}
```
6. 编写一个Servlet,查询数据库中的所有商品信息并返回给客户端。
```java
public class ProductListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 查询数据库中的所有商品信息
response.getWriter().println(products);
}
}
```
7. 编写一个Servlet,从客户端接收用户名和密码并验证是否正确。
```java
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");
// 从数据库中查询用户信息并验证
if (isValidUser) {
response.getWriter().println("登录成功");
} else {
response.getWriter().println("用户名或密码错误");
}
}
}
```
8. 编写一个Servlet,从客户端接收用户ID并删除对应的用户信息。
```java
public class DeleteUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int userId = Integer.parseInt(request.getParameter("userId"));
// 从数据库中删除用户信息
response.getWriter().println("用户删除成功");
}
}
```
9. 编写一个Servlet,从客户端接收商品ID并删除对应的商品信息。
```java
public class DeleteProductServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int productId = Integer.parseInt(request.getParameter("productId"));
// 从数据库中删除商品信息
response.getWriter().println("商品删除成功");
}
}
```
10. 编写一个Servlet,从客户端接收用户ID和新密码并更新对应的用户密码。
```java
public class ChangePasswordServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int userId = Integer.parseInt(request.getParameter("userId"));
String newPassword = request.getParameter("newPassword");
// 更新用户密码
response.getWriter().println("密码更新成功");
}
}
```
阅读全文