本系列文章是作者暑假给学生进行实训分享的笔记,主要介绍MyEclipse环境下JSP网站开发,包括JAVA基础、网页布局、数据库基础、Servlet、前端后台数据库交互、DAO等知识。
前一篇文章讲解了通过Servlet获取所提交的数据,这篇文章将详细讲解MyEclipse+Servlet+JSP实现火车票管理系统的查询页面、模糊查询、修改车票信息、删除车票等操作。基础性文章,希望对读者有所帮助 ,尤其是我的学生。
参考前文:
Java+MyEclipse+Tomcat (一)配置过程及jsp网站开发入门
Java+MyEclipse+Tomcat (二)配置Servlet及简单实现表单提交
Java+MyEclipse+Tomcat (三)配置MySQL及查询数据显示在JSP网页中
Java+MyEclipse+Tomcat (四)Servlet提交表单和数据库操作
Java+MyEclipse+Tomcat (五)DAO和Java Bean实现数据库和界面分开操作
Java+MyEclipse+Tomcat (六)详解Servlet和DAO数据库增删改查操作
前文:
[JSP暑假实训] 一.MyEclipse安装及JAVA基础知识
[JSP暑假实训] 二.JSP网站创建及Servlet实现注册表单提交、计算器运算
[JSP暑假实训] 三.MySQL数据库基本操作及Servlet网站连接显示数据库信息
下载地址:https://download.csdn.net/download/eastmount/11293780
1.新建Web工程,命名为“test0706_hcp”。
2.在工程中新建“imgs”文件夹,并放入图片及连接MySQL的jar包。
3.创建数据库hcp,新建表info,SQL语句如下所示:
-- ----------------------------
-- Table structure for `info`
-- ----------------------------
DROP TABLE IF EXISTS `info`;
CREATE TABLE `info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`start` varchar(40) DEFAULT NULL,
`end` varchar(40) DEFAULT NULL,
`name` varchar(40) DEFAULT NULL,
`starttime` datetime DEFAULT NULL,
`price` float DEFAULT NULL,
`other` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of info
-- ----------------------------
INSERT INTO `info` VALUES ('1', 'guiyang', 'kunming', 'T61', '2019-06-29 12:00:00', '56', null);
INSERT INTO `info` VALUES ('2', 'guiyang', 'kaili', 'T88', '2019-06-29 12:00:00', '26', null);
INSERT INTO `info` VALUES ('3', '贵阳', '西安', 'T98', '2019-07-04 12:00:00', '126', null);
INSERT INTO `info` VALUES ('4', '贵阳', '武汉', 'T34', '2019-07-04 12:00:00', '86', null);
INSERT INTO `info` VALUES ('5', '贵定', '西安', 'T51', '2019-07-04 12:00:00', '134', null);
INSERT INTO `info` VALUES ('6', '昆明', '武汉', 'T21', '2019-06-29 12:00:00', '123', null);
4.修改index.jsp网页布局,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>火车票管理系统title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
head>
<body>
<div align="center">
<img src="imgs/bg.jpg" height="200" width="80%" /><br />
<hr width="90%" color="red" size="3" />
<h2>火车票查询h2>
<form method="post" action="index.jsp">
起始地: <input type="text" name="start" size="20" />
发车时间:<input type="date" name="sj" size="20" />
<input type="submit" name="sub" value="查询">
form>
<br /><br />
<table border="2" width="80%">
<tr>
<td>序号td><td>起始地td><td>目的地td>
<td>车次td><td>时间td><td>价格td><td>备注td>
tr>
table>
<br />
<hr width="90%" color="red" size="3" />
©2019 YXZ 使用订票系统前必读 意见反馈 贵ICP证0000号 <br />
贵州财经大学信息学院杨秀璋及16级软工所有 贵财0000000000001号
div>
body>
html>
此时运行结果如下图所示:
5.在 index.jsp 中添加连接数据库的JDBC代码,并进行起始地模糊查询、时间查询。
完整代码如下:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>火车票管理系统title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
head>
<body>
<div align="center">
<img src="imgs/bg.jpg" height="200" width="80%" /><br />
<hr width="90%" color="red" size="3" />
<h2>火车票查询h2>
<%
//驱动的名称
String driverName = "com.mysql.jdbc.Driver";
//数据库用户名密码
String userName = "root";
String userPwd = "123456";
//数据库名字
String dbName = "hcp";
//表名
String tableName = "info";
//拼接字符串链接数据库
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" +userPwd +
"&useUnicode=true&characterEncoding=UTF-8";
//链接数据库
Class.forName("com.mysql.jdbc.Driver").newInstance();
//设置编码方式
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//获取查询的值
String start = request.getParameter("start");
System.out.println(start);
String sj = request.getParameter("sj");
System.out.println(sj);
//SQL查询
ResultSet re;
Connection con = DriverManager.getConnection(url);
Statement statement = con.createStatement();
String sql;
//起始地为空判断
if(start==null||start=="") {
if(sj==null||sj=="") { //查询所有结果
sql = "select * from " + tableName;
System.out.println(sql);
re = statement.executeQuery(sql);
} else { //按时间查询
sql = "select * from " + tableName +
" where date(starttime)='" + sj+ "';";
System.out.println(sql);
re = statement.executeQuery(sql);
}
}
else {
if(sj==null||sj=="") { //按起始点模糊查询
sql = "select * from " + tableName +
" where start like '%" + start + "%';";
System.out.println(sql);
re = statement.executeQuery(sql);
} else { //两个字段查询
sql = "select * from " + tableName +
" where start like '%" +
start + "%' and date(starttime)='" + sj+ "';";
System.out.println(sql);
re = statement.executeQuery(sql);
}
}
%>
<table border="2" width="80%">
<tr>
<td>序号td><td>起始地td><td>目的地td>
<td>车次td><td>时间td><td>价格td><td>备注td>
tr>
<%
while(re.next()) {
%>
<tr>
<td><% out.print(re.getString(1)); %>td>
<td><% out.print(re.getString(2)); %>td>
<td><% out.print(re.getString(3)); %>td>
<td><% out.print(re.getString(4)); %>td>
<td><% out.print(re.getString(5)); %>td>
<td><% out.print(re.getString(6)); %>td>
<td><% out.print(re.getString(7)); %>td>
tr>
<%
} //end while
//关闭连接
re.close();
statement.close();
con.close();
%>
table>
<br />
<hr width="90%" color="red" size="3" />
©2019 YXZ 使用订票系统前必读 意见反馈 贵ICP证0000号 <br />
贵州财经大学信息学院杨秀璋及16级软工所有 贵财0000000000001号
div>
body>
html>
初始化显示结果如下所示:
select * from info;
模糊查询结果:
select * from info where start like ‘%贵%’;
时间查询:
select * from info where date(starttime)=‘2019-06-29’;
多字段查询:
select * from info where start like ‘%贵阳%’ and date(starttime)=‘2019-07-04’;
写到这里,查询功能基本实现,接下来补充常见的错误:
(1)中文编码错误是最常见的错误,需要全部设置为“UTF-8”,包括数据库、JSP、JAVA等。
(2)这里使用的传递参数为“post”方法,同时部分学生会出现SQL语句正常显示,但无反馈结果,此时设置url需要增加编码,如下:
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" +userPwd +
"&useUnicode=true&characterEncoding=UTF-8";
(3)JAVA在调用SQL语句时,注意字符串拼接,建议同学们用 System.out.print 输出SQL语句,再观察其是否错误。尤其是from和where前后的空格、单引号等,其拼接代码如下:
sql = "select * from " + tableName + " where start like '%" +
start + "%' and date(starttime)='" + sj+ "';";
(4)while循环输出结果时,注意 { 和 } 之间的内容,它们把table布局嵌入其间。
<%
while(re.next()) {
%>
<tr>
<td><% out.print(re.getString(1)); %></td>
<td><% out.print(re.getString(2)); %></td>
<td><% out.print(re.getString(3)); %></td>
<td><% out.print(re.getString(4)); %></td>
<td><% out.print(re.getString(5)); %></td>
<td><% out.print(re.getString(6)); %></td>
<td><% out.print(re.getString(7)); %></td>
</tr>
<%
} //end while
//关闭连接
re.close();
statement.close();
con.close();
%>
1.继续修改 index.jsp 主页内容,如下所示:
<table border="2" width="80%">
<tr align="center">
<td>序号td><td>起始地td><td>目的地td>
<td>车次td><td>时间td><td>价格td><td>备注td>
<td>详情td><td>删除td><td>更新td>
tr>
<%
while(re.next()) {
%>
<tr align="center">
<td><% out.print(re.getString(1)); %>td>
<td><% out.print(re.getString(2)); %>td>
<td><% out.print(re.getString(3)); %>td>
<td><% out.print(re.getString(4)); %>td>
<td><% out.print(re.getString(5)); %>td>
<td><% out.print(re.getString(6)); %>td>
<td><% out.print(re.getString(7)); %>td>
<td><a href="Show.jsp?id=<% out.print(re.getString(1)); %>">详情a>td>
<td><a href="DeleteServlet?id=<% out.print(re.getString(1)); %>">删除a>td>
<td><a href="Update.jsp?id=<% out.print(re.getString(1)); %>">更新a>td>
tr>
<%
} //end while
//关闭连接
re.close();
statement.close();
con.close();
%>
table>
此时显示界面如下所示,注意 < a href=“DeleteServlet?id=<% out.print(re.getString(1)); %>”>删除 a> 表示跳转到DeleteServlet页面,id是传递的参数,对应数据库火车票表的主键ID(序号)。
2.创建DeleteServlet类,如下所示。
3.接着在 DeleteServlet.java 中撰写代码,导入 java.sql. * 扩展包,并将连接数据库的代码复制到 doGet()函数中。它会提示如下所示错误,数据库相关操作需要放置在try/catch异常捕获中,双击它就能自动加载try-catch。
完整代码如下所示,主要是doGet()函数:
package servlet;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DeleteServlet extends HttpServlet {
public DeleteServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取URL传递参数
int id = Integer.valueOf(request.getParameter("id"));
//驱动的名称
String driverName = "com.mysql.jdbc.Driver";
//数据库用户名密码
String userName = "root";
String userPwd = "123456";
//数据库名字
String dbName = "hcp";
//表名
String tableName = "info";
//拼接字符串链接数据库
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" +userPwd +
"&useUnicode=true&characterEncoding=UTF-8";
//链接数据库
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
//设置编码方式
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//SQL查询
ResultSet re;
Connection con = DriverManager.getConnection(url);
Statement statement = con.createStatement();
String sql = "delete from " + tableName +
" where id='" + id + "'";
System.out.println(sql);
//执行SQL语句
statement.execute(sql);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
out.println("返回
");
out.println(" ");
out.println("");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" ");
out.println("");
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}
此时运行代码,点击“删除”操作,可能会报错误如下所示:
这是因为跳转路径的问题,有两种处理方法:
< td >< a href=“DeleteServlet?id=<% out.print(re.getString(1)); %>”>删除 a> td >
方法一:修改 web.xml 文件中的URL访问路径,即除去 “servlet/” 字段。
方法二:创建Servlet类时,设置它的URL访问路径,如下图所示。
4.运行代码,成果实现删除功能。
新建 Show.jsp 页面,编辑代码如下所示:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Show.jsp' starting pagetitle>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
head>
<body>
<div align="center">
<img src="imgs/bg.jpg" height="200" width="80%" /><br />
<hr width="90%" color="red" size="3" />
<h2>火车票信息更新h2>
<%
//驱动的名称
String driverName = "com.mysql.jdbc.Driver";
//数据库用户名密码
String userName = "root";
String userPwd = "123456";
//数据库名字
String dbName = "hcp";
//表名
String tableName = "info";
//拼接字符串链接数据库
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" +userPwd +
"&useUnicode=true&characterEncoding=UTF-8";
//链接数据库
Class.forName("com.mysql.jdbc.Driver").newInstance();
//设置编码方式
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//获取查询的值
String start = request.getParameter("start");
System.out.println(start);
String sj = request.getParameter("sj");
System.out.println(sj);
//SQL查询
ResultSet re;
Connection con = DriverManager.getConnection(url);
Statement statement = con.createStatement();
int id = Integer.valueOf(request.getParameter("id"));
String sql = "select * from " + tableName + " where id=" + id;
System.out.println(sql);
re = statement.executeQuery(sql);
while(re.next()) {
%>
<form action="index.jsp" method="get">
<table border="0" width="30%">
<tr align="left">
<td>车次td>
<td><input type="text" name="upcc" readonly="readonly"
value="<% out.print(re.getString(2)); %>">td>
tr>
<tr align="left">
<td>起始点td>
<td><input type="text" name="upstart" readonly="readonly"
value="<% out.print(re.getString(3)); %>">td>
tr>
<tr align="left">
<td>目的地td>
<td><input type="text" name="upend" readonly="readonly"
value="<% out.print(re.getString(4)); %>">td>
tr>
<tr align="left">
<td>时间td>
<td><input type="text" name="uptime" readonly="readonly"
value="<% out.print(re.getString(5)); %>">td>
tr>
<tr align="left">
<td>价格td>
<td><input type="text" name="upprice" readonly="readonly"
value="<% out.print(re.getString(6)); %>">td>
tr>
<tr align="left">
<td>备注td>
<td><input type="text" name="upother" readonly="readonly"
value="<% out.print(re.getString(7)); %>">td>
tr>
<tr align="center">
<td colspan="2"><input type="submit" name="upsub" value="返回">td>
tr>
<%
}
%>
table>
form>
<hr width="90%" color="red" size="3" />
©2019 YXZ 使用订票系统前必读 意见反馈 京ICP证030173号 <br />
贵州财经大学信息学院YXZ所有 京公网安备11000002000001号
div>
body>
html>
运行结果如下图所示,均为只读。点击“返回”又继续返回主页,需要注意的是URL包含id值,即:
http://desktop-2ptb11k:8080/test0706_hcp/Show.jsp?id=1
PS:比如新闻详情页面,通常都会用这种方式,显示的结果直接用文本内容即可,而不像这里的input控件。
1创建一个Update.jsp文件,然后添加如下代码,其中表单为:
< form action=“UpdateServlet” method=“get” >。
完整代码如下:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Update.jsp' starting pagetitle>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
head>
<body>
<div align="center">
<img src="imgs/bg.jpg" height="200" width="80%" /><br />
<hr width="90%" color="red" size="3" />
<h2>火车票信息更新h2>
<%
//驱动的名称
String driverName = "com.mysql.jdbc.Driver";
//数据库用户名密码
String userName = "root";
String userPwd = "123456";
//数据库名字
String dbName = "hcp";
//表名
String tableName = "info";
//拼接字符串链接数据库
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" +userPwd +
"&useUnicode=true&characterEncoding=UTF-8";
//链接数据库
Class.forName("com.mysql.jdbc.Driver").newInstance();
//设置编码方式
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//获取查询的值
String start = request.getParameter("start");
System.out.println(start);
String sj = request.getParameter("sj");
System.out.println(sj);
//SQL查询
ResultSet re;
Connection con = DriverManager.getConnection(url);
Statement statement = con.createStatement();
int id = Integer.valueOf(request.getParameter("id"));
String sql = "select * from " + tableName + " where id=" + id;
System.out.println(sql);
re = statement.executeQuery(sql);
while(re.next()) {
%>
<form action="UpdateServlet" method="get">
<table border="0" width="30%">
<tr align="left">
<td>车次td>
<td><input type="text" name="upcc" value="<% out.print(re.getString(2)); %>">td>
tr>
<tr align="left">
<td>起始点td>
<td><input type="text" name="upstart" value="<% out.print(re.getString(3)); %>">td>
tr>
<tr align="left">
<td>目的地td>
<td><input type="text" name="upend" value="<% out.print(re.getString(4)); %>">td>
tr>
<tr align="left">
<td>时间td>
<td><input type="text" name="uptime" value="<% out.print(re.getString(5)); %>">td>
tr>
<tr align="left">
<td>价格td>
<td><input type="text" name="upprice" value="<% out.print(re.getString(6)); %>">td>
tr>
<tr align="left">
<td>备注td>
<td><input type="text" name="upother" value="<% out.print(re.getString(7)); %>">td>
tr>
<tr align="left">
<td colspan="2"><input type="hidden" name="id" value="<% out.print(re.getString(1)); %>">td>
tr>
<tr align="center">
<td><input type="submit" name="upsub" value="提交">td>
<td><input type="reset" name="upreset" value="撤销">td>
tr>
<%
}
%>
table>
form>
<hr width="90%" color="red" size="3" />
©2019 YXZ 使用订票系统前必读 意见反馈 京ICP证030173号 <br />
贵州财经大学信息学院YXZ所有 京公网安备11000002000001号
div>
body>
html>
注意:隐藏了一个input控件作为id值获取,UpdateServlet中SQL语句需要通过它更新数据。
2.此时运行结果如下图所示,其内容是可以替换的。
比如修改信息如下所示,包括时间、票价、备注,点击“提交”按钮。
其运行结果如下所示,这是因为Servlet还没有创建。
3.创建UpdateServlet类,并添加相关代码,需要注意访问路径设置如下。
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UpdateServlet extends HttpServlet {
public UpdateServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码方式
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//驱动的名称
String driverName = "com.mysql.jdbc.Driver";
//数据库用户名密码
String userName = "root";
String userPwd = "123456";
//数据库名字
String dbName = "hcp";
//表名
String tableName = "info";
//拼接字符串链接数据库
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" +userPwd +
"&useUnicode=true&characterEncoding=UTF-8";
//链接数据库
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
//获取URL传递参数 中文乱码??????????转换
int id = Integer.valueOf(request.getParameter("id"));
String upcc = new String(request.getParameter("upcc").getBytes("ISO-8859-1"), "UTF-8"); //车次
String upstart = new String(request.getParameter("upstart").getBytes("ISO-8859-1"), "UTF-8"); //起始点
String upend = new String(request.getParameter("upend").getBytes("ISO-8859-1"), "UTF-8"); //目的地
String uptime = new String(request.getParameter("uptime").getBytes("ISO-8859-1"), "UTF-8"); //时间
String upprice = new String(request.getParameter("upprice").getBytes("ISO-8859-1"), "UTF-8"); //价格
String upother = new String(request.getParameter("upother").getBytes("ISO-8859-1"), "UTF-8"); //备注
System.out.println(upother);
//SQL查询
Connection con = DriverManager.getConnection(url);
Statement statement = con.createStatement();
String sql = "update " + tableName + " set start='" + upstart
+ "' , end='" + upend + "' , name='" + upcc
+ "' , starttime='" + uptime + "' , price='" + upprice
+ "' , other='" + upother + "' where id=" + id + ";";
System.out.println(sql);
//执行SQL语句
statement.execute(sql);
//关闭连接
statement.close();
con.close();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//重定向到
//response.sendRedirect("index.jsp");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet ");
out.println("");
out.println("");
out.println(" ");
out.println ("");
out.println(" ");
out.println("");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" ");
out.println("");
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}
此时的文件结果如下图所示:
update info set start=‘武汉’ , end=‘T34’ , name=‘贵阳’ , starttime=‘2019-07-06 12:00:00.0’ ,
price=‘188’ , other=‘这是一张很棒的火车票。’ where id=4;
输出结果如下图所示:
注意:GET方法可能存在中文编码错误,输出“???”,修改方法主要是request调用setCharacterEncoding(“UTF-8”)方法设置为UTF-8。同时,下面获取的值也转码成UTF-8。
目前虽然实现了网站的功能,但其代码非常杂乱,JSP中嵌套这JAVA,JAVA中又嵌套着HTML,而我想实现的功能是:JSP就赋值布局,显示界面;Java就负责连接数据库、数据库增删改查,处理结果再返回给JSP中显示,而不是相互嵌套的。换句话说:JSP中点击“提交”按钮,TextBox中传递出发地,Java中接着请求,数据库查询,得到的结果再返回给JSP中显示。
那怎么实现呢?
接下来我们将介绍DAO和Java Bean对JDBC进行分层、模块化的最有效两个方法。DAO(数据库操作对象,Database Access Object)是JDBC下常用模式,DAO出现之前,操作数据库的代码与业务代码都出现在Servlet或者JSP中,不利用业务代码的分离。DAO出现后,所有与数据库相关的操作全被拿到了DAO层实现,Servlet或JSP只操作Java Bean或者DAP层,而DAO层值操作数据库。
周末的深夜,呆在办公室准备明天的JAVA网站开发实训,不同的是,这次有学生陪伴。各行各业都很辛苦,996和5+2让我们更珍惜生活,学生也更应该抓住编程的时光。看到学生开始学会分享博客、分享知识,真的很高兴,又拉了一些人“入坑”,传道授业解惑,为之而努力。
夜色已深,月光打在身上,勾勒出你看书时最美的侧脸,吹灭读书灯,一身都是月,任是寻常动人,想你了。接着在办公室写代码了,fighting
(By:Eastmount 2019-07-06 夜9点 http://blog.csdn.net/eastmount/)