?? noteservlet.java
字號:
package cn.mldn.lxh.note.servlet ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
import cn.mldn.lxh.note.factory.* ;
import cn.mldn.lxh.note.vo.* ;
public class NoteServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
this.doPost(request,response) ;
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
request.setCharacterEncoding("GB2312") ;
String path = "errors.jsp" ;
// 接收要操作的參數(shù)值
String status = request.getParameter("status") ;
if(status!=null)
{
// 參數(shù)有內容,之后選擇合適的方法
// 查詢全部操作
if("selectall".equals(status))
{
try
{
request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryAll()) ;
}
catch (Exception e)
{
}
path = "list_notes.jsp" ;
}
// 插入操作
if("insert".equals(status))
{
// 1、接收插入的信息
String title = request.getParameter("title") ;
String author = request.getParameter("author") ;
String content = request.getParameter("content") ;
// 2、實例化VO對象
Note note = new Note() ;
note.setTitle(title) ;
note.setAuthor(author) ;
note.setContent(content) ;
// 3、調用DAO完成數(shù)據庫的插入操作
boolean flag = false ;
try
{
DAOFactory.getNoteDAOInstance().insert(note) ;
flag = true ;
}
catch (Exception e)
{}
request.setAttribute("flag",new Boolean(flag)) ;
path = "insert_do.jsp" ;
}
// 按ID查詢操作,修改之前需要將數(shù)據先查詢出來
if("selectid".equals(status))
{
// 接收參數(shù)
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
try
{
request.setAttribute("note",DAOFactory.getNoteDAOInstance().queryById(id)) ;
}
catch (Exception e)
{
}
path = "update.jsp" ;
}
// 更新操作
if("update".equals(status))
{
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
String title = request.getParameter("title") ;
String author = request.getParameter("author") ;
String content = request.getParameter("content") ;
Note note = new Note() ;
note.setId(id) ;
note.setTitle(title) ;
note.setAuthor(author) ;
note.setContent(content) ;
boolean flag = false ;
try
{
DAOFactory.getNoteDAOInstance().update(note) ;
flag = true ;
}
catch (Exception e)
{}
request.setAttribute("flag",new Boolean(flag)) ;
path = "update_do.jsp" ;
}
// 模糊查詢
if("selectbylike".equals(status))
{
String keyword = request.getParameter("keyword") ;
try
{
request.setAttribute("all",DAOFactory.getNoteDAOInstance().queryByLike(keyword)) ;
}
catch (Exception e)
{
}
path = "list_notes.jsp" ;
}
// 刪除操作
if("delete".equals(status))
{
// 接收參數(shù)
int id = 0 ;
try
{
id = Integer.parseInt(request.getParameter("id")) ;
}
catch(Exception e)
{}
boolean flag = false ;
try
{
DAOFactory.getNoteDAOInstance().delete(id) ;
flag = true ;
}
catch (Exception e)
{}
request.setAttribute("flag",new Boolean(flag)) ;
path = "delete_do.jsp" ;
}
}
else
{
// 則表示無參數(shù),非法的客戶請求
}
request.getRequestDispatcher(path).forward(request,response) ;
}
};
/*
<servlet>
<servlet-name>note</servlet-name>
<servlet-class>cn.mldn.lxh.note.servlet.NoteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>note</servlet-name>
<url-pattern>/note/note_mvc/Note</url-pattern>
</servlet-mapping>
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -