Servlet搭建Javaweb项目
一、项目目录结构
二、代码展示
1 package com.zlj.bean; 2 3 public class Message { 4 5 private int id; 6 private String name; 7 private String email; 8 private String message; 9 10 11 //构造方法 12 13 public Message() { //无参构造 14 super(); 15 } 16 17 public Message(int id, String name, String email, String message) { 18 this.id = id; 19 this.name = name; 20 this.email = email; 21 this.message = message; 22 } 23 //set get 方法 24 public int getId() { 25 return id; 26 } 27 28 public void setId(int id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public String getEmail() { 41 return email; 42 } 43 44 public void setEmail(String email) { 45 this.email = email; 46 } 47 48 public String getMessage() { 49 return message; 50 } 51 52 public void setMessage(String message) { 53 this.message = message; 54 } 55 56 @Override 57 public String toString() { 58 return "Message{" + 59 "id=" + id + 60 ", name='" + name + '\'' + 61 ", email='" + email + '\'' + 62 ", message='" + message + '\'' + 63 '}'; 64 } 65 }
1 package com.zlj.dao; 2 3 import com.zlj.bean.Message; 4 5 import java.util.List; 6 7 public interface MessageDao { 8 List<Message> messageDao(); 9 }
1 package com.zlj.dao; 2 3 import com.sun.xml.internal.bind.v2.model.core.ID; 4 import com.zlj.bean.Message; 5 import com.zlj.connection.LinkDb; 6 import com.zlj.connection.connection; 7 8 import java.sql.PreparedStatement; 9 import java.sql.ResultSet; 10 import java.sql.SQLException; 11 import java.util.LinkedList; 12 import java.util.List; 13 14 public class MessageDaoImpl implements MessageDao { 15 16 @Override 17 public List<Message> messageDao() { 18 19 LinkDb linkDb = new LinkDb(); 20 linkDb.LinkDB(); 21 String sql = "select * from message"; 22 List<Message> resultList = new LinkedList<>(); 23 try { 24 PreparedStatement pst = linkDb.conn.prepareStatement(sql); 25 ResultSet resultSet = pst.executeQuery(); 26 while (resultSet.next() == true){ 27 Message message = new Message(); 28 message.setEmail(resultSet.getString("name")); 29 message.setId(resultSet.getInt("id")); 30 message.setName(resultSet.getString("name")); 31 message.setMessage(resultSet.getString("message")); 32 resultList.add(message); 33 } 34 }catch (SQLException e){ 35 e.getMessage(); 36 } 37 38 return resultList; 39 } 40 }
1 package com.zlj.service; 2 3 import com.zlj.bean.Message; 4 5 import java.util.List; 6 7 public interface MessageService { 8 9 List<Message> searchMessage();//查询message 10 }
1 package com.zlj.service; 2 3 import com.zlj.bean.Message; 4 import com.zlj.dao.MessageDao; 5 import com.zlj.dao.MessageDaoImpl; 6 7 import java.util.List; 8 9 public class MessageServiceImpl implements MessageService { 10 @Override 11 public List<Message> searchMessage() { 12 13 MessageDao messageDao = new MessageDaoImpl(); 14 return messageDao.messageDao(); 15 } 16 }
1 package com.zlj.servlet; 2 3 import com.zlj.bean.Message; 4 import com.zlj.service.MessageService; 5 import com.zlj.service.MessageServiceImpl; 6 import net.sf.json.JSONArray; 7 import net.sf.json.JSONObject; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 import java.io.IOException; 16 import java.io.PrintWriter; 17 import java.text.SimpleDateFormat; 18 import java.util.List; 19 20 public class MessageServlet extends HttpServlet { 21 @Override 22 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { 23 24 res.setContentType("application/json;charset=utf-8"); 25 res.setCharacterEncoding("UTF-8"); 26 try { 27 MessageService messageService = new MessageServiceImpl(); 28 List<Message> resultList = messageService.searchMessage(); 29 for (Message message : resultList) { 30 System.out.println(message); 31 } 32 JSONObject resultObject = new JSONObject(); 33 JSONArray resultArray = new JSONArray(); 34 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM 35 simpleDateFormat.setLenient(false); 36 for (Message message : resultList) { 37 resultObject.put("name", message.getName()); 38 resultObject.put("message", message.getMessage()); 39 resultObject.put("id", message.getId()); 40 resultObject.put("email", message.getEmail()); 41 resultArray.add(resultObject); 42 } 43 JSONObject object = new JSONObject(); 44 object.put("code", 200); 45 object.put("starus", true); 46 object.put("msg", "获取成功"); 47 object.put("list", resultArray); 48 PrintWriter out = res.getWriter(); 49 out.print(object); 50 out.flush(); 51 out.close(); 52 } 53 catch (IOException e){ 54 e.getMessage(); 55 } 56 System.out.println("接口请求--zlj\n\n"); 57 } 58 59 @Override 60 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 61 this.doGet(req,resp); 62 } 63 }
1 package com.zlj.connection; 2 3 public interface connection { 4 5 public void LinkDB();//连接数据库 6 }
1 package com.zlj.connection; 2 3 import java.sql.DriverManager; 4 import java.sql.SQLException; 5 import java.sql.Connection; 6 7 8 public class LinkDb implements connection { 9 10 11 public Connection conn; 12 private String DB_URL="jdbc:mysql://localhost:3306/sale?useUnicode=true&characterEncoding=UTF-8"; 13 private String USER="root"; 14 private String PASS="aaaa123456"; 15 16 @Override 17 public void LinkDB() { //实现连接书库的方法 18 try { 19 Class.forName("com.mysql.cj.jdbc.Driver"); 20 conn = DriverManager.getConnection(DB_URL,USER,PASS); 21 }catch (ClassNotFoundException e){ 22 e.printStackTrace(); 23 } catch (SQLException throwables){ 24 throwables.printStackTrace(); 25 } 26 } 27 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 7 8 <servlet> 9 <servlet-name>ServletTest</servlet-name> 10 <servlet-class>com.zlj.test.ServletTest</servlet-class> 11 </servlet> 12 13 <servlet-mapping> 14 <servlet-name>ServletTest</servlet-name> 15 <url-pattern>/servlettest</url-pattern> 16 </servlet-mapping> 17 18 19 20 <servlet> 21 <servlet-name>MessageServlet</servlet-name> 22 <servlet-class>com.zlj.servlet.MessageServlet</servlet-class> 23 </servlet> 24 25 <servlet-mapping> 26 <servlet-name>MessageServlet</servlet-name> 27 <url-pattern>/message</url-pattern> 28 </servlet-mapping> 29 </web-app>
三、浏览器请求
四、项目用到的jar包
[常用jar包介绍](https://www.dandelioncloud.cn/article/details/1534515353262780417)
[1.commons-beanutils-1.8.3.jar的介绍](https://www.yiibai.com/commons_collections/commons_collections_overview.html)
[2.commons-collections-3.2.1.jar的介绍](https://www.yiibai.com/commons_collections/commons_collections_overview.html)
[3.commons-lang-2.6.jar](https://www.cnblogs.com/stepforeward/p/16873154.html)
[4.commons-logging-1.1.1.jar](https://www.cnblogs.com/wuyongyin/p/12421153.html)
[5.ezmorph-1.0.6.jar](https://www.cnblogs.com/timssd/p/5753654.html)
[6.javax.servlet.jar和javax.servlet-api的区别](https://zhidao.baidu.com/question/180732714630314164.html)
五、数据库数据