首页 > 代码库 > java:jdbc+servlet+jsp+mysql
java:jdbc+servlet+jsp+mysql
先在mysql新增数据库和表先,把下面的几句代码复制去到mysql运行就可以创建成功了!
创建数据库
create database jdbc01 character set utf8 collate utf8_general_ci;
创建表:
use jdbc01;
create table users(
id int primary key,
name varchar(40),
password varchar(40),
email varchar(60),
birthday date,
sex boolean
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
运行的效果:
一、创建MVC架构的Web项目
与mysql 需要的包是
项目所需要的包 |
|||
序号 |
包名 |
描述 |
所属层次 |
1 |
zhu.jdbc.domain |
相当于数据库的某张表 (只包含简单的属性以及属性对应的get和set方法,不包含具体的业务处理方法),提供给【数据访问层】、【业务处理层】、【Web层】来使用 |
domain(域模型)层 |
2 |
zhu.jdbc.dao |
存放访问数据库的操作接口类 |
数据访问层 |
3 |
zhu.jdbc.dao.imp |
存放访问数据库的操作接口的实现类 |
|
4 |
zhu.jdbc.service |
存放处理系统业务接口类 |
业务处理层 |
5 |
zhu.jdbc.service.imp |
存放处理系统业务接口的实现类 |
|
6 |
zhu.jdbc.command |
相当于数据库命令的 增删查改 |
相当于三层架构的(DB层) |
7 |
zhu.jdbc.unit |
存放系统的通用工具类,提供给【数据访问层】、【业务处理层】、【DB层】来使用 |
|
8 |
zhu.jdbc.servlet |
|
Web层(相当于界面层) |
创建好的项目如下图(图-1)所示:
图-1
二、代码的编写
1. zhu.jdbc.unit的包编写
在zhu.jdbc.unit包下创建一个UnitMysql的类(这个类是建立与mysql的链接)
UnitMysql代码如下:
1 package zhu.jdbc.unit; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class UnitMysql { 10 public static final String URL="jdbc:mysql://localhost:3306/jdbc01";//链接的mysql 11 public static final String NAME = "root"; 12 public static final String PASSWORD = "root"; 13 public static final String DREIVER = "com.mysql.jdbc.Driver"; 14 15 16 static { 17 try { 18 //加载驱动器 19 Class.forName(DREIVER); 20 21 /*//另一种方法,但是会加载驱动二次,通常不是该方法 22 try { 23 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 24 } catch (SQLException e) { 25 e.printStackTrace(); 26 }*/ 27 } catch (ClassNotFoundException e) { 28 e.printStackTrace(); 29 } 30 } 31 32 public static Connection getConnection() { 33 try { 34 return DriverManager.getConnection(URL, NAME, PASSWORD);//创建与数据库的链接 35 } catch (SQLException e) { 36 e.printStackTrace(); 37 } 38 return null; 39 } 40 //设置一个公共的关闭链接、释放资源的方法 . 因为每次只要进行了增,删,查,改 之后 都必须要 事件, 那么就设置一个公共的方法 41 //而关闭资源要从 ResultSet先关闭-->,再到 PreparedStatement-->,最后到 Connection关闭 42 public static void Close(ResultSet rs, PreparedStatement ps, Connection conn) { 43 if (rs != null) { 44 try { 45 rs.close(); 46 } catch (SQLException e) { 47 e.printStackTrace(); 48 } 49 } 50 51 if (ps != null) { 52 try { 53 ps.close(); 54 } catch (SQLException e) { 55 e.printStackTrace(); 56 } 57 } 58 59 if (conn != null) { 60 try { 61 conn.close(); 62 } catch (SQLException e) { 63 e.printStackTrace(); 64 } 65 } 66 } 67 }
2. 相当于三层架构的(DB层)
在zhu.jdbc.command包下创建一个IDaoCommand的类(这个类是建立与mysql的链接)
IDaoCommand代码如下:
1 package zhu.jdbc.command; 2 3 import java.util.List; 4 5 public interface IDaoCommand<T> { 6 //查询所有的数据 7 public List<T> queryAllData(); 8 //新增数据 9 public int insertData(T t); 10 //修改数据 11 public int update(T t); 12 //删除数据 13 public int delete(int id); 14 //查询一条数据通过ID 15 public T queryDataById(int id); 16 }
3. domain(域模型)层
在zhu.jdbc.domain 包下创建一个Tb_User 类
Tb_User 代码如下:
1 package zhu.jdbc.domain; 2 3 import java.sql.Date; 4 //这里相当于数据库的某张表 5 public class Tb_User { 6 private int id; 7 private String name; 8 private String password; 9 private String email; 10 private Date birthday; 11 private boolean sex; 12 13 public boolean isSex() { 14 return sex; 15 } 16 public void setSex(boolean sex) { 17 this.sex = sex; 18 } 19 public int getId() { 20 return id; 21 } 22 public void setId(int id) { 23 this.id = id; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public String getPassword() { 32 return password; 33 } 34 public void setPassword(String password) { 35 this.password = password; 36 } 37 public String getEmail() { 38 return email; 39 } 40 public void setEmail(String email) { 41 this.email = email; 42 } 43 public Date getBirthday() { 44 return birthday; 45 } 46 public void setBirthday(Date birthday) { 47 this.birthday = birthday; 48 } 49 }
4.开发数据访问层(dao、dao.impl)
2.1: dao 继承 command的命令
在zhu.jdbc.dao包下创建一个ITb_User接口类,对于开发接口类,我习惯以字母I作类的前缀.
ITb_User代码如下:
1 package zhu.jdbc.dao; 2 3 import zhu.jdbc.command.IDaoCommand; 4 import zhu.jdbc.domain.Tb_User; 5 6 /** 7 * 这里这个类 是为了 ,后续 添加自己需要的方法. 如:模糊查询, 分页查询.... 8 * 这个必须要继承ICommand类,那样就可以调用增删查改的方法了 9 * @author Xiao_Zhu 10 * 11 */ 12 public interface ITb_User extends IDaoCommand<Tb_User> { 13 //自己需要的方法 14 }
2.2: dao.ImpI 实现 dao
在zhu.jdbc.dao.imp包下创建一个ITb_UserImpI类
ITb_UserImpI代码如下:
1 package zhu.jdbc.dao.imp; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 7 import java.sql.SQLException; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import zhu.jdbc.dao.ITb_User; 12 import zhu.jdbc.domain.Tb_User; 13 import zhu.jdbc.unit.UnitMysql; 14 15 /** 16 * 实现 ITb_User类 17 * 18 * @author Xiao_Zhu 19 * 20 */ 21 public class ITb_UserImpI implements ITb_User { 22 public Connection conn1 = null; 23 public ResultSet rs = null; 24 public PreparedStatement ps = null; 25 26 // 查询所有的数据 27 @Override 28 public List<Tb_User> queryAllData() { 29 conn1 = UnitMysql.getConnection();// 链接数据库 30 List<Tb_User> list = new ArrayList<Tb_User>(); 31 try { 32 String sqlSelect = "select * from users "; // 查询多条数据 33 ps = conn1.prepareStatement(sqlSelect); 34 rs = ps.executeQuery(); 35 Tb_User user = null; 36 while (rs.next()) { 37 user = new Tb_User(); 38 user.setId(rs.getInt("id")); 39 user.setName(rs.getString("name")); 40 user.setPassword(rs.getString("password")); 41 user.setBirthday(rs.getDate("birthday")); 42 user.setSex(rs.getBoolean("sex")); 43 list.add(user); 44 } 45 } catch (SQLException e) { 46 e.printStackTrace(); 47 } finally { 48 UnitMysql.Close(rs, ps, conn1); 49 } 50 return list; 51 } 52 53 // 新增 54 @Override 55 public int insertData(Tb_User t) { 56 conn1 = UnitMysql.getConnection(); 57 int i = 0; 58 try { 59 String sqlInsert = "insert into users(name,password,email,birthday,sex) values(?,?,?,?,?) ;"; 60 ps = conn1.prepareStatement(sqlInsert, 61 PreparedStatement.RETURN_GENERATED_KEYS); 62 // 这里的1,2..必须要按上面的新增的顺序来定义 63 ps.setString(1, t.getName()); 64 ps.setString(2, t.getPassword()); 65 ps.setString(3, t.getEmail()); 66 ps.setDate(4, new java.sql.Date(t.getBirthday().getTime())); 67 ps.setBoolean(5, t.isSex()); 68 ps.executeUpdate(); 69 rs = ps.getGeneratedKeys();// 得到 最新的 ID 70 if (rs.next()) {// 是否存在 71 i = rs.getInt(1); 72 } 73 } catch (SQLException e) { 74 e.printStackTrace(); 75 } finally { 76 UnitMysql.Close(rs, ps, conn1); 77 } 78 return i; 79 } 80 81 // 修改 82 @Override 83 public int update(Tb_User t) { 84 conn1 = UnitMysql.getConnection(); 85 int i = 0; 86 try { 87 String sqlUpdate = "update users set name=?, password =? ,sex=? where id=?"; 88 ps = conn1.prepareStatement(sqlUpdate); 89 ps.setString(1, t.getName()); 90 ps.setString(2, t.getPassword()); 91 ps.setBoolean(3, t.isSex()); 92 ps.setInt(4, t.getId()); 93 i = ps.executeUpdate(); 94 } catch (SQLException e) { 95 e.printStackTrace(); 96 } finally { 97 UnitMysql.Close(null, ps, conn1); 98 } 99 return i; 100 } 101 102 // 删除 103 @Override 104 public int delete(int id) { 105 conn1 = UnitMysql.getConnection(); 106 int i = 0; 107 try { 108 String sqlDelete = "delete from users where id=?"; 109 ps = conn1.prepareStatement(sqlDelete); 110 ps.setInt(1, id); 111 i = ps.executeUpdate(); 112 if (i == 1) { 113 return i; 114 } 115 } catch (SQLException e) { 116 e.printStackTrace(); 117 } finally { 118 UnitMysql.Close(null, ps, conn1); 119 } 120 return i; 121 } 122 123 // 查询一条数据通过ID 124 @Override 125 public Tb_User queryDataById(int id) { 126 conn1 = UnitMysql.getConnection(); 127 String sql = "select * from users where id=?"; 128 Tb_User user = null; 129 if (id > 0) { 130 try { 131 ps = conn1.prepareStatement(sql); 132 ps.setInt(1, id); 133 rs = ps.executeQuery(); 134 if (rs.next()) { 135 user = new Tb_User(); 136 user.setId(rs.getInt("id")); 137 user.setName(rs.getString("name")); 138 user.setPassword(rs.getString("password")); 139 user.setBirthday(rs.getDate("birthday")); 140 user.setSex(rs.getBoolean("sex")); 141 142 } 143 } catch (SQLException e) { 144 e.printStackTrace(); 145 } finally { 146 UnitMysql.Close(null, ps, conn1); 147 } 148 } 149 return user; 150 } 151 152 }
5. 业务处理层(service,service.imp)
- .service层
在zhu.jdbc.service包下创建一个ITb_UserService 类
ITb_UserService 如下:
1 package zhu.jdbc.service; 2 3 import zhu.jdbc.command.IServiceCommand; 4 import zhu.jdbc.domain.Tb_User; 5 6 7 public interface ITb_UserService extends IServiceCommand<Tb_User> { 8 //这里与dao层的中的ITb_User.java是一样的意思 9 }
1.1 这里继承的 IServiceCommand 类是在zhu.jdbc.command下 创建一个IServiceCommand的类
代码IServiceCommand 如下:
1 package zhu.jdbc.command; 2 3 import java.util.List; 4 5 6 public interface IServiceCommand<T> { 7 //查询所有的数据 8 public List<T> queryAllData(); 9 //新增数据 10 public boolean insertData(T t); 11 //修改数据 12 public boolean update(T t); 13 //删除数据 14 public boolean delete(int id); 15 //查询一条数据通过ID 16 public T queryDataById(int id); 17 }
2. service.imp层
//这里的 service层要想与 dao层(BAL层逻辑层)建立联系那么必须要 创建 dao层的对象
在zhu.jdbc.service.imp包下创建一个ITb_UserServiceImpI 类
ITb_UserServiceImpI 代码如下:
1 package zhu.jdbc.service.imp; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.util.List; 7 8 import zhu.jdbc.dao.ITb_User; 9 import zhu.jdbc.dao.imp.ITb_UserImpI; 10 import zhu.jdbc.domain.Tb_User; 11 import zhu.jdbc.service.ITb_UserService; 12 /** 13 * 链接与dao层的链接 14 * @author Xiao_Zhu 15 * 16 */ 17 public class ITb_UserServiceImpI implements ITb_UserService{ 18 public Connection conn1 = null; 19 public ResultSet rs = null; 20 public PreparedStatement ps = null; 21 boolean b=false; 22 //这里的 service层要想玉 dao层(BAL层逻辑层)建立联系那么必须要 创建 dao层的对象 23 ITb_User myiTb_User=new ITb_UserImpI();//创建了 dao层的ITb_UserImp对象 24 25 //查询所有数据 26 @Override 27 public List<Tb_User> queryAllData() { 28 29 return myiTb_User.queryAllData(); 30 } 31 32 //新增 33 @Override 34 public boolean insertData(Tb_User t) { 35 if (t!=null) { 36 if (t!=null) { 37 myiTb_User.insertData(t); 38 b=true; 39 } 40 } 41 return b; 42 } 43 44 //修改 45 @Override 46 public boolean update(Tb_User t) { 47 if (t!=null) { 48 if (t!=null) { 49 myiTb_User.update(t); 50 b=true; 51 } 52 } 53 return b; 54 } 55 //删除 56 @Override 57 public boolean delete(int id) { 58 if (id!=0) { 59 myiTb_User.delete(id); 60 b=true; 61 } 62 return b; 63 } 64 //查询一条数据 65 @Override 66 public Tb_User queryDataById(int id) { 67 if (id!=0) { 68 return myiTb_User.queryDataById(id); 69 } 70 else { 71 return null; 72 } 73 } 74 75 }
6. Web层(相当于界面层)
在zhu.jdbc.servlet包下创建一个Servlet_TbUser 类
创建的Servlet_TbUser类要在web配置
web的配置内容如下:
1 2 <!--Servlet_TbUser.java的配置--> 3 <servlet> 4 <servlet-name>Servlet_TbUser</servlet-name> 5 <servlet-class>zhu.jdbc.servlet.Servlet_TbUser</servlet-class> 6 </servlet> 7 <servlet-mapping> 8 <servlet-name>Servlet_TbUser</servlet-name> 9 <url-pattern>/zhu/Servlet_TbUser</url-pattern> 10 </servlet-mapping>
Servlet_TbUser 代码如下:
1 package zhu.jdbc.servlet; 2 3 import java.io.IOException; 4 import java.sql.Date; 5 import java.text.ParseException; 6 import java.text.SimpleDateFormat; 7 import java.util.List; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import zhu.jdbc.domain.Tb_User; 15 import zhu.jdbc.service.ITb_UserService; 16 import zhu.jdbc.service.imp.ITb_UserServiceImpI; 17 18 public class Servlet_TbUser extends HttpServlet { 19 20 /** 21 * 22 */ 23 private static final long serialVersionUID = 1L; 24 25 //这里建立 与service层的 联系 创建一个service层imp的某个的对 26 ITb_UserService myITb_UserService=new ITb_UserServiceImpI(); 27 28 @Override 29 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 30 throws ServletException, IOException { 31 doPost(req, resp); 32 } 33 34 @Override 35 protected void doPost(HttpServletRequest request, HttpServletResponse response) 36 throws ServletException, IOException { 37 request.setCharacterEncoding("UTF-8");//解决乱码 38 String type=request.getParameter("who"); 39 //新增 40 if("Insert".equals(type)){ 41 Insert(request, response); 42 } 43 else if("update".equals(type)){ 44 update(request, response); 45 } 46 else if("queryById".equals(type)){ 47 queryById(request, response); 48 } 49 else if("delete".equals(type)){ 50 delete(request, response); 51 } 52 else if("queryAll".equals(type)){ 53 queryAll(request, response); 54 } 55 } 56 //新增 57 public void Insert(HttpServletRequest request, HttpServletResponse response) 58 throws ServletException, IOException { 59 //这里jsp中name专递过来的参数 60 String name=request.getParameter("name"); 61 String birthday=request.getParameter("birthday"); 62 String password=request.getParameter("password"); 63 String email=request.getParameter("email"); 64 String sex=request.getParameter("sex"); 65 //把获取到的这些值放到user里 66 Tb_User user =new Tb_User(); 67 68 try { 69 //下面两句是把 string 转换为 sql类型的 时间格式 70 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 71 user.setBirthday(new Date(sdf.parse(birthday).getTime())); 72 } catch (ParseException e1) { 73 e1.printStackTrace(); 74 } 75 76 user.setEmail(email); 77 user.setName(name) ; 78 user.setPassword(password); 79 if ("1".equals(sex)) { 80 user.setSex(true); 81 } 82 else if ("0".equals(sex)) { 83 user.setSex(false); 84 } 85 //最后调用服务来添加 86 String message=null; 87 if (myITb_UserService.insertData(user)==true) { 88 queryAll(request, response); 89 } 90 else { 91 message="新增失败!!!"; 92 request.setAttribute("msg", message); 93 request.getRequestDispatcher("/index.jsp").forward(request, response); 94 } 95 96 97 } 98 //修改 99 public void update(HttpServletRequest request, HttpServletResponse response) 100 throws ServletException, IOException { 101 String name=request.getParameter("name"); 102 String birthday=request.getParameter("birthday"); 103 String password=request.getParameter("password"); 104 String email=request.getParameter("email"); 105 String sex=request.getParameter("sex"); 106 String id=request.getParameter("id"); 107 //把获取到的这些值放到user里 108 Tb_User user =new Tb_User(); 109 try { 110 //下面两句是把 string 转换为 sql类型的 时间格式 111 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 112 user.setBirthday(new Date(sdf.parse(birthday).getTime())); 113 } catch (ParseException e1) { 114 e1.printStackTrace(); 115 } 116 user.setId(Integer.parseInt(id)); 117 user.setEmail(email); 118 user.setName(name) ; 119 user.setPassword(password); 120 if ("1".equals(sex)) { 121 user.setSex(true); 122 } 123 else if ("0".equals(sex)) { 124 user.setSex(false); 125 } 126 boolean b= myITb_UserService.update(user); 127 if (b==true) { 128 queryAll(request, response); 129 } 130 else { 131 request.setAttribute("msg", "修改失败!!"); 132 request.getRequestDispatcher("/index.jsp").forward(request, response); 133 } 134 request.getRequestDispatcher("/jsp/User.jsp").forward(request, response); 135 } 136 //查询一条数据 137 public void queryById(HttpServletRequest request, HttpServletResponse response) 138 throws ServletException, IOException { 139 Tb_User user=null; 140 String id= request.getParameter("id"); 141 System.out.println(id); 142 user= myITb_UserService.queryDataById(Integer.parseInt(id) ); 143 request.setAttribute("user", user); 144 request.getRequestDispatcher("/jsp/User.jsp").forward(request, response); 145 } 146 //删除 147 public void delete(HttpServletRequest request, HttpServletResponse response) 148 throws ServletException, IOException { 149 String id= request.getParameter("id"); 150 System.out.println(id); 151 boolean message=myITb_UserService.delete(Integer.parseInt(id)); 152 if (message==true) { 153 queryAll(request, response); 154 } 155 else { 156 157 request.setAttribute("msg", "删除失败!!"); 158 request.getRequestDispatcher("/index.jsp").forward(request, response); 159 } 160 } 161 //查询所有的数据 162 public void queryAll(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ 163 List<Tb_User> lis=myITb_UserService.queryAllData(); 164 request.setAttribute("list", lis); 165 request.getRequestDispatcher("/jsp/User.jsp").forward(request, response); 166 } 167 168 }
三、创建jsp
User的代码如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 %>
8 <!-- c标签要使用,那么就必须要有它 --> 9 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 10 <c:set scope="page" var="url" 11 value="${pageContext.request.contextPath }"></c:set>
12 13 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 14 <html> 15 <head> 16 <base href="<%=basePath%>"> 17 18 <title>新增用户</title> 19 20 <meta http-equiv="pragma" content="no-cache"> 21 <meta http-equiv="cache-control" content="no-cache"> 22 <meta http-equiv="expires" content="0"> 23 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 24 <meta http-equiv="description" content="This is my page"> 25 26 27 </head> 28 29 <body> 30 <div align="center" 31 style="width: 400px; position: relative;left:450px"> 32 <form action="${url}/zhu/Servlet_TbUser?who=Insert" method="post"> 33 <h4>新增用户</h4> 34 姓名: <input type="text" name="name"><br /> 35 密码: <input type="text" name="password"><br /> 36 出生日期 : <input type="text" name="birthday"><br /> 37 性别: <select name="sex"> 38 <option value="0">男</option> 39 <option value="1">女</option> 40 </select><br /> 41 <input type="submit" value="新增"/> 42 <hr /> 43 </form> 44 </div> 45 <div align="center"style="width: 400px; position: relative;left:450px;"> 46 <form action="${url}/zhu/Servlet_TbUser?who=queryAll" method="post"> 47 <input type="submit" value="查询所有的数据"/> <br/> 48 <table border="1" cellspacing="0"> 49 <thead> 50 <tr><td>ID</td><td>姓名</td><td>密码</td><td>日期</td><td>性别</td><td>操作</td></tr> 51 </thead> 52 <tbody> 53 <c:forEach items="${list}" var="list"> 54 <tr> 55 <td>${list.id }</td> 56 <td>${list.name }</td> 57 <td>${list.password }</td> 58 <td>${list.birthday }</td> 59 <td><c:if test="${list.sex==false }">男</c:if> 60 <c:if test="${list.sex==true }">女</c:if></td> 61 <td><a href= "${url}/zhu/Servlet_TbUser?who=queryById&id=${list.id}" style=‘text-decoration:none‘ onclick=‘update(this)‘ >修改 </a> 62 <a href= "${url}/zhu/Servlet_TbUser?who=delete&id=${list.id}" style=‘text-decoration:none‘ >删除</a> </td> 63 </tr> 64 </c:forEach> 65 </tbody> 66 </table> 67 <hr /> 68 </form> 69 </div> 70 <div align="center" 71 style="width: 400px; position: relative;left:450px"> 72 <form action="${url}/zhu/Servlet_TbUser?who=Insert" method="post"> 73 <h4>修改用户</h4> 74 姓名: <input type="text" name="name" value="${user.name }"><br /> 75 密码: <input type="text" name="password" value="${user.password }"><br /> 76 出生日期 : <input type="text" name="birthday" value="${user.birthday }"><br /> 77 性别: <select name="sex" > 78 <option value="0">男</option> 79 <option value="1">女</option> 80 </select><br /> 81 <input type="submit" value="保存修改"/> 82 <hr /> 83 </form> 84 </div> 85 </body> 86 </html>
index.jsp代码如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP ‘index.jsp‘ starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> 20 --> 21 </head> 22 23 <body> 24 <h1>${msg }</h1> <br> 25 </body> 26 </html>
四、运行效果:
源码下载地址: http://pan.baidu.com/s/1eSkpqHs
java:jdbc+servlet+jsp+mysql