javaEE综合项目 servlet+jsp

servlet :负责处理 jsp:负责显示
MobileServlet.java

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mobile.dao.MobileDao;
import mobile.daoImpl.MobileDaoImpl;
import mobile.entity.Mobile;

public class MobileServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public MobileServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置请求编码,防止中文乱码
		request.setCharacterEncoding("UTF-8");
		//获取method参数的值,他就是Servlet的方法名
		String methodname=request.getParameter("method");
		try {
			// 根据方法名,反射一个Method对象
			Method method = this.getClass().getDeclaredMethod(methodname,
					HttpServletRequest.class, HttpServletResponse.class);
			// 通过Method对象,动态的调用方法
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void queryAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		MobileDao dao = new MobileDaoImpl();
		List<Mobile> list = dao.queryAll();  
		// 将集合存到request域中
		request.setAttribute("mobs", list);
		// 跳转到list.jsp页面
		request.getRequestDispatcher("list.jsp").forward(request, response);
		System.out.println("全查询方法被调用");
	}
	
	// 处理添加的请求
		public void add(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {

			// 获取表单中传过来的参数的值
			String brand = request.getParameter("brand");
			String model = request.getParameter("model");
			double price = Double.parseDouble(request.getParameter("price"));
			int totalnum = Integer.parseInt(request.getParameter("totalnum"));
			// 调用Dao中的添加方法
			Mobile mob = new Mobile();
			mob.setBrand(brand);
			mob.setModel(model);
			mob.setPrice(price);
			mob.setTotalnum(totalnum);

			MobileDao dao = new MobileDaoImpl();
			int i = dao.insert(mob);
			if (i > 0) {
				// 跳转到全查询的servlet,不能直接跳到list.jsp
				response.sendRedirect("mob.action?method=queryAll");
				// this.queryAll(request, response);
			}

		}
		public void delete(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			
			int id = Integer.parseInt(request.getParameter("id"));
			MobileDao dao = new MobileDaoImpl();
			int i = dao.delete(id);
			if (i > 0) {
				// 跳转到全查询的servlet,不能直接跳到list.jsp
				response.sendRedirect("mob.action?method=queryAll");
			}
		}
	
		// 准备修改,做修改前的查询
		public void toUpdate(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			// 根据id查询该记录
			int id = Integer.parseInt(request.getParameter("id"));
			System.out.println("我被调用了吗?");
			MobileDao dao = new MobileDaoImpl();
			Mobile mobile = dao.queryById(id);
			// 把查到的学生对象存到request域中
			request.setAttribute("mob", mobile);
			// 跳转到update.jsp页面
			request.getRequestDispatcher("update.jsp").forward(request, response);
		}

		// 处理修改请求
		public void update(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			// 获取id
			int id = Integer.parseInt(request.getParameter("id"));
			String barnd = request.getParameter("brand");
			String model = request.getParameter("model");
			double price = Double.parseDouble(request.getParameter("price"));
			int totalnum = Integer.parseInt(request.getParameter("totalnum"));
			// 调用Dao中的修改方法
			Mobile mob= new Mobile();
			  mob.setId(id);
			  mob.setBrand(barnd);
			  mob.setModel(model);
			  mob.setPrice(price);
			  mob.setTotalnum(totalnum);
			

			MobileDao dao = new MobileDaoImpl();
			int i = dao.update(mob);
			if (i > 0) {
				// 跳转到全查询的servlet,不能直接跳到list.jsp
				response.sendRedirect("mob.action?method=queryAll");
			}
		}

}

index.jsp

<body>
       <a href="mob.action?method=queryAll">显示所有手机信息</a>
  </body>

list.jsp

<body>
  		<h2>手机列表</h2>
	
	<table width="50%" border="1" cellspacing="0" cellpadding="10">
		<tr>
			<td>手机编号</td>
			<td>品牌</td>
			<td>型号</td>
			<td>价格</td>
			<td>总数</td>
			<td>操作</td>
		</tr>
		<c:forEach var="mob" items="${mobs}">
		<tr>
			<td>${mob.id }</td>
			<td>${mob.brand }</td>
			<td>${mob.model }</td>
			<td>${mob.price }</td>
			<td>${mob.totalnum }</td>
          <td>
            <!-- 如果点超链接时,需要响应js事件,那么建议把href写成
            javascript:void(0)或者javascript:;表示让超链接不作任何跳转,只响应js事件 -->
          <a href="javascript:void(0)" onclick="deleteById(${mob.id})" >删除</a>
           
		  <a href="mob.action?method=toUpdate&id=${mob.id}">修改</a></td>
			
		
	</tr>
		</c:forEach>
		
	</table>
    <a href="add.jsp">添加手机</a>
    <script>
    	function deleteById(id){
    			 // confirm()函数会弹出一带确认和取消按钮的对话框,如果点击确认按钮,函数返回true,否则返回false
         var flag=  confirm('是否确认删除此记录'); 
    	 if(flag){
    		//alert(id);
    		//发送请求给doDelete.jsp页面, location.href是改变请求的路径,发送get请求
    	   
    	     location.href="mob.action?method=delete&id="+id;
    	 }
    	}
    </script>
     <br>
  </body>

add.jsp

body>
  <h2>添加手机页面</h2>
    <form action="mob.action?method=add" method="post">
    品牌:<input type="text" name="brand"> <br>
    型号:<input type="text" name="model"> <br>
    价格:<input type="text" name="price"> <br>
    总数:<input type="text" name="totalnum"> <br>
  <input type="submit" value="添加新的手机">
     <br>
     </from>
  </body>

update.jsp

<body>
  <h2> 修改手机的页面</h2>
    
    
     <form action="mob.action?method=update" method="post">
      <!-- 将学生的id存到只读的文本框中,就不能被修改了,也可以将id存到隐藏域中 -->
 <input type="text" name="id" value="${mob.id}" readonly><br>
  <%-- <input type="hidden" name="id" value="${mob.id}"><br> --%>
    品牌:<input type="text" name="brand" value="${mob.brand}"> <br>
    型号:<input type="text" name="model" value="${mob.model}"> <br>
    价格 :<input type="text" name="price" value="${mob.price}"> <br>
    总数:<input type="text" name="totalnum" value="${mob.totalnum}"> <br>
 <input  type="submit"  value="修改">
     </form>
  </body>

猜你喜欢

转载自blog.csdn.net/qq_41532872/article/details/87388695
今日推荐