项目中各类文件的路径
代码部分:
html 文件
<body>
<div id="all">
<div class="window">
<form action="NS" method="post">
标题:<input type="text" name="title">
<p></p>
金额:<input type="text" name="price">
<p></p>
<input type="submit" name="caozuo" value="creat">
</form>
</div>
</div>
</body>
Notes_Servlet.java
package org.jsoft.servlet;
import java.io.IOException;
import java.text.ParseException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jsoft.dao.Notes_DAO;
import org.jsotf.vo.Notes_Vo;
/**
* Servlet implementation class Notes_servlet
*/
public class Notes_Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Notes_Servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//<a> 默认的是get 所以要调用doPost方法
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//取表单值
request.setCharacterEncoding("UTF-8");
String caozuo=request.getParameter("caozuo");
//根据操作来执行相应的代码
System.out.println(caozuo);
if(caozuo.equals("creat"))
{
String title=request.getParameter("title");
String price1=request.getParameter("price");
//转换成double 类型
double price=Double.parseDouble(price1);
Notes_Vo nv=new Notes_Vo();
nv.setnTitle(title);
nv.setnPrice(price);
//填照片
String image="image/a.jpg";
nv.setnPhoto(image);
Notes_DAO nd=new Notes_DAO();
try {
if(nd.inseret(nv)){
//如果插入成功 则返回表单所有的信息
request.getRequestDispatcher("Notes_info.jsp").forward(request, response);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//执行删除的代码
else if(caozuo.equals("del")){
//取得ID
String nId1=request.getParameter("nID");
int nId=Integer.parseInt(nId1);
//根据ID 删除该行信息
Notes_Vo nv=new Notes_Vo();
nv.setnId(nId);
Notes_DAO nd=new Notes_DAO();
if(nd.delUser(nv)){
//删除成功 跳转到信息页面
request.getRequestDispatcher("Notes_info.jsp").forward(request, response);
}
}
//执行修改的代码
else if(caozuo.equals("update")){
request.setCharacterEncoding("UTF-8");
String nID1=request.getParameter("nID");
int nID=Integer.parseInt(nID1);
String nPrice1=request.getParameter("price");
double nPrice=Double.parseDouble(nPrice1);
String nTitle=request.getParameter("title");
//将表中的信息 赋值NotesVo中
Notes_Vo nvo=new Notes_Vo();
nvo.setnId(nID);
nvo.setnPrice(nPrice);
nvo.setnTitle(nTitle);
//调用修改的方法
Notes_DAO ndao=new Notes_DAO();
if(ndao.updateUser(nvo)){
//修改成功 跳转到信息页面
request.getRequestDispatcher("Notes_info.jsp").forward(request, response);
}
}
}
}
Notes_Update.jsp
<%@page import="org.jsoft.dao.Notes_DAO"%>
<%@page import="org.jsotf.vo.Notes_Vo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<form action="NS?nID=<%=request.getParameter("nID")%>&&caozuo=update" name="caozuo" method="post">
<table>
<tr>
<td>用户编号</td>
<td>标题</td>
<td>赏金</td>
</tr>
<tr>
<td><input type="text" name="nID" value="<%= request.getParameter("nID")%>" disabled="disabled"></td>
<td><input type="text" name="title" value="<%= request.getParameter("title")%>"></td>
<td><input type="text" name="price" value="<%= request.getParameter("price")%>"></td>
<td> <input type="submit" value="确定修改"></td>
</tr>
</table>
</form>
</body>
</html>
Notes_info.jsp
<%@page import="org.jsotf.vo.Notes_Vo"%>
<%@page import="java.util.List"%>
<%@page import="org.jsoft.dao.Notes_DAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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 border="1" cellspacing ="0">
<tr>
<td>用户编号</td>
<td>日期</td>
<td>照片</td>
<td>赏金</td>
<td>标题</td>
<td>操作</td>
</tr>
<%
//执行 查询的java代码块
//并遍历输出
Notes_DAO ndao=new Notes_DAO();
List<Notes_Vo> ln=ndao.selectNotes();
for(Notes_Vo nv :ln){
%>
<tr>
<td><%= nv.getnId()%></td>
<td><%= nv.getnCreatDate()%></td>
<td><img src="<%= nv.getnPhoto()%>"></td>
<td><%= nv.getnPrice()%></td>
<td><%= nv.getnTitle()%></td>
<td><a href="NS?caozuo=del&&nID=<%=nv.getnId()%>" name="caozuo">删除</a></td>
<td><a href="Notes_Update.jsp?nID=<%= nv.getnId()%>&price=<%= nv.getnPrice()%>&title=<%= nv.getnTitle()%>">修改</a></td>
</tr>
<% }%>
</table>
</body>
</html>
效果图:
\