model2之Servlet控制器

目录结构:

代码部分:

1.Product类

package app.domain;


import java.io.Serializable;


public class Product implements Serializable{
      private String name;
      private String description;
      private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
      

}

2.ProductForm类

package app.form;



public class ProductForm {
    private String name;
    private String description;
    private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDecription() {
return description;
}
public void setDecription(String description) {
this.description = description;
}
public String getPrice() {
return price;
}
public void setPrice(String productPrice) {
this.price = productPrice;
}
    

}

3.controllerServlet类

package app.Servlet;
import java.io.IOException;


import javax.servlet.RequestDispatcher;
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 app.domain.Product;
import app.form.ProductForm;


/**
 * Servlet implementation class controllerServlet
 */
@WebServlet(name="controllerServlet",urlPatterns= {"/add","/saved"})
public class controllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public controllerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
process(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

process(request,response);
}
/*
*  <lable for="description">description:</lable>
          <input type="text" id="description" name="description">
          </p>
          <!-- 表单价格进行填写 -->
          <p>
          <lable for="price:">price:</lable>
          <input type="text" id="price" name="price">
*/
private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取uri
String uri=request.getRequestURI();
System.out.println(uri);
//获取最后一个反斜杠  /  位置
int lastf=uri.lastIndexOf("/");
uri=uri.substring(lastf+1);
//action
if(uri.equals("saved")) {//如果为提交的表单则进行保存
System.out.println(uri);
       String ProductName=request.getParameter("name");
       String ProductDesription=request.getParameter("description");
       String ProductPrice=request.getParameter("price");
       //添加到productform对象中
       //创建form
       ProductForm productform=new ProductForm();
       productform.setName(ProductName);
       productform.setDecription(ProductDesription);
       productform.setPrice(ProductPrice);
      
       
       //保存到Product
       //创建model
       Product product=new Product();
       product.setName(productform.getName());
       product.setDescription(productform.getDecription());
       product.setPrice(Float.parseFloat(productform.getPrice()));
       
       
       //保存model  for view
       request.setAttribute("product", product);
       //response.seta
}else {
System.out.println("pppp!!!");
}


//forward to view
String dispatchUrl=null;
if(uri.equals("add")){
dispatchUrl="WEB-INF/jsp/ProductForm.jsp";
System.out.println("访问form");
}else {
dispatchUrl="WEB-INF/jsp/formSaved.jsp";
}
RequestDispatcher rd=null;
if(dispatchUrl!=null) {
rd=request.getRequestDispatcher(dispatchUrl);
}
rd.forward(request, response);
}


}

jsp页面:

1.填写表单:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <div>
    <!--创建表单  -->
       <form action="saved">
          <fieldset>
          <!--表单功能进行描述  -->
          <legend>Add a product</legend>
          <!--表单名名称进行填写  -->
          <table>
          <tr>
           <td><lable for="name">Product name:</lable></td>
            <td><input type="text" id="name" name="name"></td>
          </tr>
          <!-- 表单详情填写 -->
          <tr>
          <td><lable for="description">description:</lable></td>
          <td><input type="text" id="description" name="description"></td>
          </tr><br/>
          <!-- 表单价格进行填写 -->
          <tr>
          <td><lable for="price:">price:</lable></td>
          <td><input type="text" id="price" name="price"></td>
          </tr><br/>
          <!-- 提交与重置 -->
          <tr id="buttons">
              <td><input id="reset" type="reset"/></td>
              <td><input id="submit" type="submit"/></td>
          </tr><br/>
          </table>
          </fieldset>
       </form>
    </div>
</body>

</html>

2.提交反馈:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Saved product details</title>
</head>
<body>
    <div id="global">
        <h4>product has saved!!!</h4>
        <p>
           <h5>Details</h5>
           product Name:${product.name}<br/>
           Product Description:${product.description}<br/>
           Product Price:${product.price}<br/>
        </p>
    </div>
</body>

</html>

结果:





猜你喜欢

转载自blog.csdn.net/qq_32067151/article/details/80085909