
2012年7月11日
1 SQL脚本
CREATE TABLE `tb_goods1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`price` float DEFAULT NULL,
`unit` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`manufacturer` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
)
2 创建网站
3 新建Servlet 名称是GoodsServlet 包名是com.jht.servlet
4 引用Tomcat, 附加jar包文件 <Build Path菜单>
mysql-connector-java-3.0.16-ga-bin :数据库操作类
jstl-api-1.2 JSP标准标签库
jstl-impl-1.2 JSP标准标签库
5 创建实体类 GoodsForm 包名是 com.jht.model
代码如下:
public class GoodsForm {
private int id = 0; // 编号属性
private String name = ""; // 商品名称属性
private float price = 0.0f; // 单价属性
private String unit = ""; // 单位属性
private String manufacturer = ""; // 厂商属性
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPrice(float price) {
this.price = price;
}
public float getPrice() {
return price;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getUnit() {
return unit;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getManufacturer() {
return manufacturer;
}
}
6 编写数据库连接与操作类
package com.jht.tools;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class ConnDB {
public Connection conn=null;
public Statement stmt=null;
public ResultSet rs=null;
private static String propFileName="connDB.properties";
private static Properties prop=new Properties();
private static String dbClassName="com.mysql.jdbc.Driver";
private static String dbUrl="jdbc:mysql://127.0.0.1:3306/db_Database07?user=root&password=111&characterEncoding=UTF-8";
public ConnDB() { //定义构造方法
try { //捕捉异常
//将Properties文件读取到InputStream对象中
InputStream in = getClass().getResourceAsStream(propFileName);
prop.load(in); // 通过输入流对象加载Properties文件
dbClassName = prop.getProperty("DB_CLASS_NAME"); // 获取数据库驱动
dbUrl = prop.getProperty("DB_URL", dbUrl); //获取URL
} catch (Exception e) {
e.printStackTrace(); // 输出异常信息
}
}
public static Connection getConnection() {
Connection conn = null;
try { //连接数据库时可能发生异常因此需要捕捉该异常
Class.forName(dbClassName).newInstance(); //装载数据库驱动
//建立与数据库URL中定义的数据库的连接
conn = DriverManager.getConnection(dbUrl);
} catch (Exception ee) {
ee.printStackTrace(); //输出异常信息
}
if (conn == null) {
System.err
.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"
+ dbClassName
+ "\r\n链接位置:"
+ dbUrl); //在控制台上输出提示信息
}
return conn; //返回数据库连接对象
}
/*
* 功能:执行查询语句
*/
public ResultSet executeQuery(String sql) {
try { // 捕捉异常
conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql); //执行SQL语句,并返回一个ResultSet对象rs
} catch (SQLException ex) {
System.err.println(ex.getMessage()); // 输出异常信息
}
return rs; // 返回结果集对象
}
/*
* 功能:关闭数据库的连接
*/
public void close() {
try { // 捕捉异常
if (rs != null) { // 当ResultSet对象的实例rs不为空时
rs.close(); // 关闭ResultSet对象
}
if (stmt != null) { // 当Statement对象的实例stmt不为空时
stmt.close(); // 关闭Statement对象
}
if (conn != null) { // 当Connection对象的实例conn不为空时
conn.close(); // 关闭Connection对象
}
} catch (Exception e) {
e.printStackTrace(System.err); // 输出异常信息
}
}
}
7 配置文件connDB.properties 内容如下:
DB_CLASS_NAME=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://127.0.0.1:3306/c2cd?user=root&password=root&characterEncoding=UTF-8
8 web.xml 文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>GoodsServlet</servlet-name>
<servlet-class>com.jht.servlet.GoodsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GoodsServlet</servlet-name>
<url-pattern>/GoodsServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
9 index.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:redirect url="GoodsServlet">
<c:param name="action" value="query" />
</c:redirect>
</body>
</html>
10 GoodsList.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="450" height="47" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#333333">
<tr>
<td height="30" colspan="5" bgcolor="#EFEFEF">·商品列表</td>
</tr>
<tr>
<td width="36" height="27" align="center" bgcolor="#FFFFFF">编号</td>
<td width="137" align="center" bgcolor="#FFFFFF">商品名称</td>
<td width="85" align="center" bgcolor="#FFFFFF">单价</td>
<td width="38" align="center" bgcolor="#FFFFFF">单位</td>
<td width="148" align="center" bgcolor="#FFFFFF">厂商</td>
</tr>
<c:forEach var="goods" items="${requestScope.goodsList}">
<tr>
<td height="27" bgcolor="#FFFFFF">
<c:out value="${goods.id}"/></td>
<td bgcolor="#FFFFFF">
<c:out value="${goods.name}"/></td>
<td bgcolor="#FFFFFF">
<c:out value="${goods.price}"/>(元)</td>
<td bgcolor="#FFFFFF">
<c:out value="${goods.unit}"/></td>
<td bgcolor="#FFFFFF">
<c:out value="${goods.manufacturer}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
11 显示数据库中的商品信息
posted @
2012-07-11 14:35 jhtchina 阅读(397) |
评论 (0) |
编辑 收藏

2012年7月4日
1 创建test1 Dynamic Web Project站点
2 创建Servlet 设置java package(com.servlet)和class name(MyServlet)。
设置:
选择Add Library
选择tomcat

3 在WebContent/WEB-INF 下面创建web.xml文件。
Web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="
http://java.sun.com/xml/ns/javaee"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>MyServlet</welcome-file>
</welcome-file-list>
</web-app>
4 编写MyServlet.java代码
import java.io.PrintWriter;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out=response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>servlet sample</TITLE></HEAD>");
out.println("<BODY>");
out.println("servlet 实例 ");
out.println(this.getClass());
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
5 WebContent下面创建index.jsp
Body里面增加代码<jsp:forward page="MyServlet"></jsp:forward>
6 运行结果如下:
servlet 实例 class com.servlet.MyServlet
备注:
MyServlet.java代码修改
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.print("<p>上下文路径:" + request.getServletPath() + "</p>");
out.print("<p>HTTP请求类型:" + request.getMethod() + "</p>");
out.print("<p>请求参数:" + request.getQueryString() + "</p>");
out.print("<p>请求URI:" + request.getRequestURI() + "</p>");
out.print("<p>请求URL:" + request.getRequestURL().toString() + "</p>");
out.print("<p>请求Servlet路径:" + request.getServletPath() + "</p>");
out.flush();
out.close();
运行结果:
上下文路径:/MyServlet
HTTP请求类型:GET
请求参数:null
请求URI:/test1/MyServlet
请求URL:http://localhost:8080/test1/MyServlet
请求Servlet路径:/MyServlet
注意:Web Deployment Assembly增加Add,对mysql.jar的引用
posted @
2012-07-04 16:32 jhtchina 阅读(118) |
评论 (0) |
编辑 收藏
t10.jsp
<%@ page language="java" contentType="text/html; charset=gbk" errorPage="11.jsp"
pageEncoding="gbk"%>
<!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=gbk">
<title>Insert title here</title>
</head>
<body>
<%
int apple=Integer.parseInt("ghj");
System.out.println("apple:"+apple);
%>
</body>
</html>
t11.jsp
<%@ page language="java" contentType="text/html; charset=gbk" isErrorPage="true"
pageEncoding="gbk"%>
<%@ page import="java.util.*" %>
<!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=gbk">
<title>Insert title here</title>
</head>
<body>
<%=exception.getMessage() %>
</body>
</html>
posted @
2012-07-04 14:58 jhtchina 阅读(166) |
评论 (0) |
编辑 收藏
Application 提供了对应用程序环境属性访问的方法。
在WEB-INF目录下面新建web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param> <!-- 定义连接数据库URL -->
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/db_database15</param-value>
</context-param>
<context-param> <!-- 定义连接数据库用户名 -->
<param-name>name</param-name>
<param-value>root</param-value>
</context-param>
<context-param> <!-- 定义连接数据库mim -->
<param-name>password</param-name>
<param-value>111</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
t9.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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=gbk">
<title>Insert title here</title>
<base href="<%=basePath%>">
</head>
<body>
<%
String url = application.getInitParameter("url"); //获取初始化参数,与web.xml文件中内容对应
String name = application.getInitParameter("name");
String password = application.getInitParameter("password");
out.println("URL: "+url+"<br>");
out.println("name: "+name+"<br>");
out.println("password: "+password+"<br>");
%>
</body>
</html>
posted @
2012-07-04 14:39 jhtchina 阅读(166) |
评论 (0) |
编辑 收藏
t7.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ page import="javax.servlet.http.*" %>
<!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=GBK">
<title>Insert title here</title>
</head>
<body>
<%
String welcome="第一次访问";
String[] info=new String[]{"","",""};
Cookie[] cook=request.getCookies();
if (cook!=null)
{
for(int i=0;i<cook.length;i++)
{
if (cook[i].getName().equals("myCookInfo"))
{
info=cook[i].getValue().split("#");
welcome=",欢迎回来!";
}
}
}
%>
<%=info[0]+welcome %>
<form action="t8.jsp" method="post">
<ul>
<li>
姓名:<input name="name" type="text" value="<%=info[0] %>" >
</li>
<li>
出生日期:<input name="birthday" type="text" value="<%=info[1] %>">
</li>
<li>
邮箱地址:
<input name="mail" type="text" value="<%=info[2] %>" >
</li>
<li>
<input type="submit" value="提交">
</li>
</ul>
</form>
</body>
</html>
t8.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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=GBK">
<title>Insert title here</title>
</head>
<body>
<%
String name =new String(request.getParameter("name").getBytes("ISO8859_1"),"GBK");
String birthday = request.getParameter("birthday");
String mail = request.getParameter("mail");
Cookie myCook = new Cookie("myCookInfo",name+"#"+birthday+"#"+mail);
myCook.setMaxAge(60*60*24*365);
response.addCookie(myCook);
%>
表单提交成功
<ul style="line-height: 24px">
<li>姓名:<%= name %>
<li>出生日期:<%= birthday %>
<li>电子邮箱:<%= mail %>
<li><a href="t7.jsp">返回</a>
</ul>
</body>
</html>
posted @
2012-07-04 14:23 jhtchina 阅读(147) |
评论 (0) |
编辑 收藏