扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
AddServlet类实现了servlet控制器的功能,但你这里只有一个add功能并不能体现控制器需要其他功能了就使用request对象获取请求资源路径针对不同的路径找到不同的Servelt实现控制器功能即MVC中的C;
创新互联公司专业IDC数据服务器托管提供商,专业提供成都服务器托管,服务器租用,IDC机房托管,IDC机房托管,成都多线服务器托管等服务器托管服务。
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddServlet extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String description = req.getParameter("description");
String baseprice = req.getParameter("baseprice");
String writer = req.getParameter("writer");
String publish = req.getParameter("publish");
String pages = req.getParameter("pages");
String images = req.getParameter("images");
String stock = req.getParameter("stock");
Product p=new Product();
ProductDao Dao = new ProductDao();
p.setName(name);
p.setDescription(description);
p.setBaseprice(baseprice);
p.setWriter(writer);
p.setPublish(publish);
p.setPages(pages);
p.setImages(images);
p.setStock(stock);
int result = 0;
try {
result = Dao.save(p);
} catch (Exception e) {
e.printStackTrace();
}
resp.setContentType("text/html");
if(result == 1){
resp.sendRedirect("ok.jsp");
}else{
resp.sendRedirect("error.jsp");
}
}
}
ProductDao类实现了使用对象想数据库中添加记录的功能;
实现了MVC中的M模型
package servlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class ProductDao {
public int save(Product p) throws Exception{
Connection conn = null;
PreparedStatement prep = null;
try {
conn = DBUtil.getConnection();
String sql = "insert into product values(0,?,?,?,?,?,?,?,?)";
prep = conn.prepareStatement(sql);
prep.setObject(1, p.getName());
prep.setObject(2, p.getDescription());
prep.setObject(3, p.getBaseprice());
prep.setObject(4, p.getWriter());
prep.setObject(5, p.getPublish());
prep.setObject(6, p.getPages());
prep.setObject(7, p.getImages());
prep.setObject(8, p.getStock());
int num=prep.executeUpdate();
return num;
} catch (Exception e1) {
e1.printStackTrace();
throw e1;
}finally{
DBUtil.close(conn);
}
}
}
Product类根据数据库的字段声明类的属性根据表名命名类名;
package servlet;
public class Product {
private Object name;
private Object description;
private Object baseprice;
private Object writer;
private Object publish;
private Object pages;
private Object images;
private Object stock;
public Object getName() {
return name;
}
public void setName(Object name) {
this.name = name;
}
public Object getDescription() {
return description;
}
public void setDescription(Object description) {
this.description = description;
}
public Object getBaseprice() {
return baseprice;
}
public void setBaseprice(Object baseprice) {
this.baseprice = baseprice;
}
public Object getWriter() {
return writer;
}
public void setWriter(Object writer) {
this.writer = writer;
}
public Object getPublish() {
return publish;
}
public void setPublish(Object publish) {
this.publish = publish;
}
public Object getPages() {
return pages;
}
public void setPages(Object pages) {
this.pages = pages;
}
public Object getImages() {
return images;
}
public void setImages(Object images) {
this.images = images;
}
public Object getStock() {
return stock;
}
public void setStock(Object stock) {
this.stock = stock;
}
}
DBUtil类封装的是对数据库的连接:
package servlet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* JDBC工具类:
* 提供了获得连接,关闭连接的相关的方法。
*
*/
public class DBUtil {
private static String url="jdbc:mysql://localhost:3306/ecport";
public static Connection getConnection() throws Exception{
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.
getConnection(url,"root", "");
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return conn;
}
public static void close(Connection conn){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Connection conn =
getConnection();
System.out.println(conn);
}
}
最后是MVC中的视图,实现的shiMVC中的V视图,在AddServlet中根据不同的结果受用重定向跳转到指定的页面中,表明是否添加成功。
成功页面:
%@page pageEncoding="utf-8"
contentType="text/html;charset=utf-8" %
html
body
h3 style='color:red'恭喜你,新增图书成功!!!/h3
/body
/html
失败页面:
%@page pageEncoding="utf-8"
contentType="text/html;charset=utf-8" %
html新增失败/html
public void setNum1(String x1) {
this.num1 = Double.parseDouble("x1");
}
"x1"不需要""
①用一个容器(拦截器或者servlet类)接受请求
②分割请求路径:得到请求访问类以及访问方法的标示字符串;
③通过字符串获得Action类对象以及方法
④执行方法:可以通过获得的返回值确定跳转的路径以及跳转方式。
⑤OVER
我刚刚写完一个很简单的(MVC+ORM)框架:
有源码以及API的,你可以看看。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流