public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String local = "/home/hadoop/Downloads"; String filePath = new String(request.getParameter("filePath").getBytes("ISO-8859-1"),"GB2312"); System.out.println(filePath); JobConf conf = HDFSDao.getConfig(); HDFSDao hdfs = new HDFSDao(conf); hdfs.download(filePath, local); FileStatus[] list = hdfs.ls("hdfs://master:9000/user/hadoop"); request.setAttribute("list", list); request.getRequestDispatcher("index.jsp").forward(request, response); } } System.out.println( "Download Successfully!" ); }
时间: 2023-06-03 22:05:39 浏览: 146
这是一个名为 DownloadServlet 的公共类,继承了 HttpServlet 类。它有一个 serialVersionUID 属性,并设置为 1L。该类实现了 HttpServlet 类的方法。
相关问题
import java.io.IOException; import ja.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if ("admin".equals(username) && "admin".equals(password)) { response.sendRedirect("success.jsp"); } else { response.sendRedirect("fail.jsp"); } } }
这段代码使用了Java Servlet技术实现了一个简单的登录功能。
代码中实现了一个名LoginServlet的Servlet类,使用@WebServlet注解将映射到了路径“/login”,当用户在浏览器中访问该路径时,就会由该Servlet类来处理请求。
在doPost方法中,首先通过request.getParameter方法获取了用户输入的用户名和密码,然后进行简单的验证,如果用户名和密码都为“admin”,则将用户重定向到success.jsp页面;否则将用户重定向到fail.jsp页面。
需要注意的是,由于Servlet是一个服务器端的技术,因此需要将该代码部署到一个Web容器中才能运行。常见的Web容器有Tomcat、Jetty等。
package com.test.servlet; import java.io.IOException; 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 javax.servlet.http.HttpSession; import com.test.bean.Cart; import com.test.dao.CartDao; /** * Servlet implementation class DelCartServlet */ @WebServlet("/Servlet/DelCartServlet") public class DelCartServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DelCartServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setHeader("Content-type","text/html;charset=utf-8") ; String gid=request.getParameter("gid"); if(gid!=null) { HttpSession session=request.getSession(); Cart cart=(Cart)session.getAttribute("cart"); if(cart!=null) { CartDao cartdao=new CartDao(); cartdao.delGoods(cart, Integer.parseInt(gid)); session.setAttribute("cart", cart); response.getWriter().println("删除成功"); }else { response.getWriter().println("删除失败"); } }else { response.getWriter().println("参数不正确"); } response.getWriter().println("<a href='"+request.getContextPath()+"/cart.jsp'>查看购物车<a>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } 修改删除功能,在购物车物品数量大于1的情况下点击“删除”时减少1个,等于1时删除此项商品;
您的要求是修改 DelCartServlet 中的删除功能,使得在购物车物品数量大于1的情况下,点击“删除”按钮时减少1个商品,等于1时删除此项商品。
为了实现这个功能,您可以在 CartDao 类中添加一个新的方法,来实现删除商品的功能。这个方法可以接收两个参数:Cart 对象和商品 ID,然后根据商品 ID 找到对应的商品,将其数量减一或删除该商品。在 DelCartServlet 中,您可以调用这个新方法实现删除商品的功能。
以下是修改后的 DelCartServlet 代码:
```
package com.test.servlet;
import java.io.IOException;
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 javax.servlet.http.HttpSession;
import com.test.bean.Cart;
import com.test.dao.CartDao;
/**
* Servlet implementation class DelCartServlet
*/
@WebServlet("/Servlet/DelCartServlet")
public class DelCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DelCartServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setHeader("Content-type","text/html;charset=utf-8") ;
String gid=request.getParameter("gid");
if(gid!=null) {
HttpSession session=request.getSession();
Cart cart=(Cart)session.getAttribute("cart");
if(cart!=null) {
CartDao cartdao=new CartDao();
int count = cartdao.delGoods(cart, Integer.parseInt(gid));
if (count > 0) {
session.setAttribute("cart", cart);
response.getWriter().println("删除成功");
} else {
response.getWriter().println("删除失败");
}
}else {
response.getWriter().println("删除失败");
}
}else {
response.getWriter().println("参数不正确");
}
response.getWriter().println("<a href='"+request.getContextPath()+"/cart.jsp'>查看购物车</a>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
```
修改后的 CartDao 类中的 delGoods 方法如下:
```
public int delGoods(Cart cart, int gid) {
int count = 0;
for (int i = 0; i < cart.getItems().size(); i++) {
if (cart.getItems().get(i).getGoods().getGid() == gid) {
if (cart.getItems().get(i).getCount() > 1) {
cart.getItems().get(i).setCount(cart.getItems().get(i).getCount() - 1);
} else {
cart.getItems().remove(i);
}
count++;
break;
}
}
return count;
}
```
这个方法会返回删除的商品数量,如果返回值大于 0,说明删除成功,否则说明删除失败。
阅读全文