javaweb笔记.docx
- 文档编号:23790892
- 上传时间:2023-05-20
- 格式:DOCX
- 页数:28
- 大小:29.04KB
javaweb笔记.docx
《javaweb笔记.docx》由会员分享,可在线阅读,更多相关《javaweb笔记.docx(28页珍藏版)》请在冰豆网上搜索。
javaweb笔记
Filter接口的方法及说明定义对象是应实现此接口
publicvoidinit(FilterCOnfigfilterConfig)过滤器的初始化方法
对<过滤器>
每一个filter的对象此方法制备调用一次
publicvoiddoFilter(ServletRequestrequetServletRreponse
response)此方法与Service的service()方法类似当请求及相应交给过
滤器是过滤器掉用此方法进行处理
publicvoiddestroy()过滤器生命周期结束时掉用此方法用于释放过
滤器所占的资源
FilterChain接口此接口由容器进行实现
voiddoFileter(SerletRequestrequest,SerletResponseresponse
)throwsIOExceptionServletException
此方法主要用于将对过滤器处理的请求与响应交给下一个过滤器对象在多
个过滤器的web应用中,用此方法进行传递
FilterConfig接口
此方法有容器进行实现用于获取过滤器初始化期间的参数信息
publicStinggetFilterName()返回过滤器的名称
publicStringgetInitParamter(Stingname)返回初始化值为name的
参数值
publicEnumerationgetInitParameterName()返回所有初始化参数名
的名句集合
publicServletCOntextgetServletContext()返回Servlet上下文对象
过滤器的配置
1生命过滤器的对象
2映射过滤器说明
filtername过滤器的名称
url-pattern制定过滤器的url样式
disppatcher用于指定过滤器对应的请求方式
dispatcher说明
REQUEST当客户端直接请求时通过过滤器进行处理
INCLUDE当客户端通过RequesDispatcher对象的include()方法进行
请求时通过过滤器进行处理
FORWARD当客户端通过RequestDispatcher对象的forward()方法请求
是通过过滤器处理
ERROR当生命产生异常时通过过滤器进行处理
最后别忘了servlet的配置很重要
-************************************************************
《jsp与servlet实现用户注册》
1.间数据表用于存储用户注册信息
2.创建RegServlet类用于处理用户注册请求(他是servlet的对象)此
类有init()和doPost()两个方法init()是servlet初始化方法此方
法只运行一次在此方法中加载数据库驱动并获得数据库连接对象
ConnectiondoPost()连接好数据库后处理用户注册信息
privateConnectionconn;
publicvoidinit()throwsServletException{
加载驱动
Class.forName("com.jdbc.mysql.Driver");
数据库连接url
Stringsql="jdbc:
mysql:
//localhost:
3306/biaoming";
h获得数据库连接
conn=Driver.getConncetion(url,用户名,密码);
以上内容写在trycatch中
}
publicvoiddoPost(HttpServeltResult
request,HttpServletResponseponse)throws
ServeltException,IOException{
设置requestresponse的编码
response.setContentType("text/html");
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
获得表单的属性
Stingusename=request.getParameter("username");..
...
判断数据库是否连接成功
if(conn!
=null){
插入注册信息的SQL语句(使用?
占位符)
Stringsql="insertintotb_user
(username,pwd,sex,question,answer.email)"+"values(?
?
?
?
?
?
)";
创建PreparedStatement对象
PreparedStatementps=conn.prepareStatement(sql);
对sql与剧中的参数进zhi行动态赋值
ps.setString(1,username);
....
执行跟新操作
ps.executeUpdate();
获得输出对象
PrintWriterout=response.getWriter();
输出结果注册信息
out.print("
out.print(username+"注册成功");
out.print("");
out.flush();
out.close();
以上内容也写在trycatch中
}
}
3.在x.xml进行servelt的配置
4.创建index。
jsp页面放了用户注册信息所需的表单
(this)"> 最后别忘了将mysql-connector-java-5.1.6-bin.jar放入你的lib中 ******************************************************* notebook实现注册登录查看留言并对留言的增删改查己留言列表的分 页 《1》entity实体对应着数据库的一张表 publicclassNote() {privateidtitlecontentauthoruidcreateTime及其getset方法} publicclassUser{ privateidnamepwdsexcreateTime及其getset方法己有参构造函 数 } 《2》dao提供了对实体的增删改查Note是方法的声明NoteImpl是方法的实现 publicinterfaceNoteDao{ publicList publicList publicvoidaddNote(NOtenote);//增加留言 publicvoidremoveNote(Integerid);//删除留言 publicvoidupdateNote(Notenote);//修改留言 publicNotefindById(Integerid);//编辑留言? ? publicList } publicinterfaceUserDao{ publicvoidaddUser(Useruser); publicvoidremover(Integerid); publicvoidupdateUser(Useruser); publicUserfinUser(Integerid);//查询用户 //根据属性查找用户 publicList publicList value,Stringtype); } publicclassNoteDaoImplimplementsNoteDao{ privateUserDaouserDao=newUserDaoImpl(); 加改除留言的方法均为 //先到数据库进行相应的操作在进行跟新 Stringsql="insertintonotevalues(null,? ? ? CURRENT_TIMESTAMP)"; DbUtil.executeUpdate(sql,note.getUid(), note.getTitle(),note.getContent()); } //查找所有留言或根据某属性查找留言先到数据库找到留言然后进行操 作 publicList Stringsql="select*fromnote"; List for(Notenode: nodes){ //得到留言记录的用户信息 node.setAuthor(userDao.findUser(node.getUid())); } returnnodes; } publicList Stringsql="select*fromnotewhereuid=? "; List (Note.class,sql,uid); for(Notenode: nodes){ //得到留言记录的用户信息 node.setAuthor(userDao.findUser(uid)); } returnnodes; } 《3》封装了业务方法Service privateNoteDaonoteDao=newNoteDaoImpl(); publicList returnnoteDao.findAll(); } publicList returnnoteDao.findAll(); } 跟新保存均是publicvoidsaveNote/updateNote(Notenote){ noteDao.saveNote(note);/updateNote(note); } 删除根据id找留言 删除保存根据属性查找跟新 publicclassUserService{ //用户注册 publicvoidaddUser(Useruser){userDao。 addUser(user);} //用户登录验证用户名密码信息 publicUservalidateUser(Stringname,Stringpwd){ List if(users.size()>0){ Useru=users.get(0); if(u.getPwd().equals(pwd)){returnu;} } }returnnull; } 《4》web由控制器有servlet组成 NoteServlet方法中先判断method的值为几何在用相应的只调用request,response Stringmethod=request.getParameter("method"); if("listNote".equals(method)){ listNote(request,response);} 然后是分页即对note的真正增删改出的代码部分 /** *显示所有留言信息,在此分页 **/ protectedvoidlistNote(HttpServletRequestrequest, HttpServletResponseresponse)throws ServletException,IOException{ StringpageNo=request.getParameter("pageNo"); if(pageNo==null){ pageNo="1"; } Pagepage=newPage (2); page.setPageNo(Integer.parseInt(pageNo)); List request.setAttribute("notes",notes); request.setAttribute("page",page); request.getRequestDispatcher ("/list_note.jsp").forward(request,response); } /** *删除留言 **/ protectedvoiddelNote(HttpServletRequestrequest, HttpServletResponseresponse)throws ServletException,IOException{ Integerid=Integer.parseInt (request.getParameter("id")); noteService.removeNote(id); listNote(request,response); } /** *增加留言 **/ protectedvoidaddNote(HttpServletRequestrequest, HttpServletResponseresponse)throws ServletException,IOException{ Useruser=(User)request.getSession ().getAttribute("user"); Stringtitle=request.getParameter("title"); Stringcontent=request.getParameter ("content"); Notenote=newNote(); note.setTitle(title); note.setContent(content); note.setUid(user.getId()); noteService.saveNote(note); listNote(request,response); } 《5》编码过滤器安全过滤器内容监听器用户数量的监听器 内容监听器 publicclassContextListenerimplementsServletContextListerner {publicvoidcontextDestroyed(ServletContextEveentarg0) } //用来进行级别初始化的函数 publicvoidcontextInitialized(ServletContextEventevent) {//得到内容保存用户名及密码 ServeltContextctx=event.getServeltContext(); cnt.setAttribute("root",ctx.getContextPath()) ;} 用户监听器 pbulicclassUserConntListenerimplements HttpSessionAttributeSrvletServletContextListener { //增加移除修改属性 publicvoidattriubteAdded(){ ServletContextapplication=event.getSession(); System.out.println("增加属性"+event.getName(); 新用户登录 if(enent.getName().equals("user")){ Integercnt=(Integer)application.ggetAttribute(cnt); application.setAtteibute("dnt",cnt+1);名字value值 } } publicvoidattriubteRemoved(){ ServletContextapplication=event.getSession(); System.out.println("移除属性"+event.getName(); Integercnt=(Integer)application.ggetAttribute(cnt); application.setAtteibute("dnt",cnt-1);名字value值 } } publicvoidattriubteReplaced(){ System.out.println("移除属性"+event.getName(); } } //字符集过滤器 publicclassEncodingFilterimplementsFilter{ privateStringencoding; privateList publicvoiddestroy(){}服务器关闭时调用 publicvoiddoFilter(requestresponsechain){ HttpServletRequestreq=(HttpServletRequest)request; Stringurl=req.getRequestURI();请求url中? 钱的内容 intindex=url.lastIndexOf("."); if(intdex>=0){ Stringpostfix=url.substing(index+1); if(excludes.contains(porfix)) { chain.doFilter(request,response); return; }} }核心部分在匹配到的每一次请求都会调用 } servelt只被调用一次他在第一次请求是调用 Filter的init植被调用一次他在服务器启动时调用 publicvoidinit(FilterConfigconfig)throwsServeltException {过滤器初始化 encoding=config.getInitParameter("encoding"); String[]ecds=config.getInitParameter("exclude"); excludes=Arrays.asList(ecds); } 《6》Dbuti数据库连接的工具类 publicclassDbutil{ 获得一个数据库的连接 publicstaticConnectiongetConnection(){ } 执行一个增删改的sql语句没有返回值 sql要执行sql语句可用? 占位符valuessql语句参数填充? 占位符 pbulicstaticvoidexecuteUpdate(Stringsql,Object...values) { } 执行一个查询 publicstatic object...values){ } 执行一个sql语句的查询并可以返回任意多个字段个多条记录返回结果及 时二维表 publicstaticList ...values) } 查询一张所有的字段 publicstaticList } 执行一个sql语句查询并可以返回任意多个字段的一条记录 publicstaticList String...values){} 《7》property文件 驱动url连接数据库的用户名己密码 driver=com.mysql.jdbc.Driver url=jdbc: mydql: //localhost: 3306/test user=root password=root jsp内置对象用法 2007-01-0412: 35 1.1 application *在jsp中获得application对象 如: getServletContext().setAttribute("counter",new mycount.Counter()); 如: useBean scope="application" id="counter" class="mycounter.Counter"/> *在jsp中处理On Application Start和On Session Start事件的方法 使用HttpSessionBindingListener类. 添加session: session.putValue("bingdings.listener",new MyListener(getServletContext()); 定义MyListener类: import javax.servlet.http.*; import javax.servlet.*; public class MyListener implements HttpSessionBindingListener{ ServletContext context; public MyListener(ServletContext context){ this.context=context; } public void valueBound(HttpSessionBindingEvent event){ System.out.println("v>executeQuery(Stringsql,String
>executeQuery(Stringtabname){
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- javaweb 笔记