servlet学习笔记9——第七讲(美化页面,完善功能)

(一)美化管理系统界面,完善页面
软件:
playcolor——得到颜色配置
Sothink Logo Maker——制作网页logo


(二)管理系统功能改进
1.删除用户

(1)wel.java中添加

pw.println("<td><a href="DelUserCl?userid=" mce_href="DelUserCl?userid=""+ub.getUserId()+" onclick=/"return window.confirm('您确认删除该用户吗?')/">删除用户</a></td>");

(2)编写DelUserCl.java(删除处理页面)

//处理删除某个用户 package com.tsinghua; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class DelUserCl extends HttpServlet { public void init() throws ServletException{} public void process(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ try{ res.setContentType("text/html;charset=UTF-8"); //中文乱码解决 //调用userBeanCl的删除用户的方法,完成删除 UserBeanCl ubc=new UserBeanCl(); //接收从Wel.java中传递的id String id=req.getParameter("userid"); if( ubc.delUser(id)){ //删除成功 res.sendRedirect("Ok"); }else{ //删除失败 res.sendRedirect("Erro"); } }catch(Exception ex){ ex.printStackTrace(); } } public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ process(req,res); } public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ process(req,res); } public void destroy(){ super.destroy(); } }

(3)编写Erro.java(删除失败)

//操作失败页面 package com.tsinghua; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Erro extends HttpServlet { public void init() throws ServletException{} public void process(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ try{ //中文乱码解决 res.setContentType("text/html;charset=UTF-8"); //业务逻辑 PrintWriter pw=res.getWriter(); //返回登录界面 pw.println("<html>"); pw.println("<body bgcolor=#345D6E>"); pw.println("<img src="images/lola.gif" mce_src="images/lola.gif"><hr><center>"); pw.println("<h1>很遗憾,操作失败!</h1>"); pw.println("<a href="main" mce_href="main">返回主界面</a>  <a href="welcome" mce_href="welcome">继续删除/修改用户</a>"); pw.println("</center><hr><img src="images/mylogo.bmp" mce_src="images/mylogo.bmp">"); pw.println("</body>"); pw.println("</html>"); }catch(Exception ex){ ex.printStackTrace(); } } public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ process(req,res); } public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ process(req,res); } public void destroy(){ super.destroy(); } }

(4)编写Ok.java(删除成功页面)

//操作成功页面 package com.tsinghua; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Ok extends HttpServlet { public void init() throws ServletException{} public void process(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ try{ //中文乱码解决 res.setContentType("text/html;charset=UTF-8"); //业务逻辑 PrintWriter pw=res.getWriter(); //返回登录界面 pw.println("<html>"); pw.println("<body bgcolor=#345D6E>"); pw.println("<img src="images/lola.gif" mce_src="images/lola.gif"><hr><center>"); pw.println("<h1>恭喜你,操作成功</h1>"); pw.println("<a href="main" mce_href="main">返回主界面</a>  <a href="welcome" mce_href="welcome">继续删除/修改用户</a>"); pw.println("</center><hr><img src="images/mylogo.bmp" mce_src="images/mylogo.bmp">"); pw.println("</body>"); pw.println("</html>"); }catch(Exception ex){ ex.printStackTrace(); } } public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ process(req,res); } public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ process(req,res); } public void destroy(){ super.destroy(); } }
2.显示用户的头像

 

(1)数据库添加字段:
    alter table users add column myPic varchar(50);
    insert into users(myPic) values('1.gif');//添加记录

(2)在images中加入头像

(3)添加代码:

pw.println("<img src="images/lola.gif" mce_src="images/lola.gif"> 欢迎您:"+myName+"<img src="images/1.gif" mce_src="images/1.gif"><hr><center>");

3.指定跳转到某一页
清楚servlet和html表单提交原理

Wel.java中加入:

pw.println("请输入需要跳转的页面"); pw.println("<form action=welcome>"); pw.println("<input type=text name=pageNow>"); pw.println("<input type=submit value=go>"); pw.println("</form>");

3.精确查找和模糊查找

//查询用户使用表单 pw.println("<form method=post action=search>"); pw.println("请输入用户名:<input type=text name=uname>"); pw.println("<input type=submit value=搜><br><br>"); pw.println("<input type=radio name=sType value=mo checked>模糊查询"); pw.println("<input tyep=radio name=sType value=zhu>精确查询"); pw.println("</form>"); //得到搜索的类型 String type=req.getParameter("sType"); String con=req.getParameter("uname"); if(con!=null){ String sql=""; if(type.equals("mo")){ sql="select * from users where username like '%'"+con+"%'";//模糊查找 }else{ sql="select * from users where username='"+con+"'";// 精确查找 } }

(三)终极框架图

servlet学习笔记9——第七讲(美化页面,完善功能)_第1张图片

 

 

你可能感兴趣的:(exception,String,servlet,Class,input,import)