MVC模式实现(下)
来源:互联网 发布:淘宝翡翠手把件真假 编辑:程序博客网 时间:2024/05/29 16:35
在这里我们接着实现web类
web类
web类里的都是servlet程序,我们需要继承HttpServlet 类,然后我们重写`doget()`和`dopost()`方法,如果手动建立的话,我们还需要在`WEB-INF/web.xml`中配置servlet的对应类和对应的映射url。我们直接在muEclipse中建立,会直接配置好,不过需要注意映射的路径,在网页访问中需要将数据提交给这两个servlet。因为我们需要处理两个需求,所以建立两个servlet类一个为`LogUserServlet`,另一个为`RegUserServlet`我们先来处理注册类。重写doget,dopost在doget里面我们直接让他执行doPost
doPost(req, resp);
然后再doPost中写我们的需求
首先需要注意的就是编码问题,这是response的编码,设置编码为utf-8,对于response设置charset=uft-8
然后我们得到每个属性,赋给一个User,判断如果服务返回true就设置消息为注册成功
代码如下:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //设置编码 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); //获取数据 String username=req.getParameter("username"); String password=req.getParameter("password"); String phone=req.getParameter("phone"); String address=req.getParameter("address"); String email=req.getParameter("email"); UserService userService=new UserServiceImpl(); try{ boolean p= userService.registerUser(new User(id, username, password, phone, address, email)); if(p){ req.setAttribute("msg", "注册成功"); id++;} else { req.setAttribute("msg", "注册失败"); } req.getRequestDispatcher("message.jsp").forward(req, resp); }catch (Exception e) { // TODO: handle exception System.out.println(e); e.printStackTrace(); req.setAttribute("msg", "服务器忙,请稍后再试"); req.getRequestDispatcher("message.jsp").forward(req, resp); } }
然后之后的RegUserServlet也是类似
判断是否能够得到user对象,如果得到,则登录成功。
代码:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub // 设置编码 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); // 获取数据 String username = req.getParameter("username"); String password = req.getParameter("password"); UserService userService = new UserServiceImpl(); try { User user = userService.Login(username, password); if (user != null) { req.getSession().setAttribute("user", user); req.setAttribute("msg", "登录成功"); id++; } else { req.setAttribute("msg", "登录失败"); } req.getRequestDispatcher("message.jsp").forward(req, resp); } catch (Exception e) { // TODO: handle exception req.setAttribute("msg", "服务器忙,请稍后再试"); req.getRequestDispatcher("message.jsp").forward(req, resp); } }
web.ui类
完成了后台之后,我们在这个包下,完成跳转的操作。
建立UserServlet类
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String ope=request.getParameter("ope"); if(ope==null){ request.getRequestDispatcher("login.jsp").forward(request, response); }else if(ope.equals("reg")){ request.getRequestDispatcher("reg.jsp").forward(request, response); } }
这样后台的服务基本完成,我们还需要建立前台的jsp页面
jsp页面
建立login.jsp页面,基本就是一个form然后提交到loginservlet即可
<h1>用户登录</h1> <form action="loginservlet"> <p>用户名: <input type="text" name="username"/> </p> <p>密码: <input type="password" name="password"/> </p> <p> <input type="submit" value="登录"/> </p> </form>
注册页面也是一个form,然后提交到requserservlet
reg.jsp
<h1>注册页面</h1> <form action="reguserservlet" method="post"> <table> <tr> <td>用户名:</td> <td> <input type="text" name="username"/> </td> </tr> <tr> <td>密码:</td> <td> <input type="password" name="password"/> </td> </tr> <tr> <td>电话:</td> <td> <input type="text" name="phone"/> </td> </tr> <tr> <td>地址:</td> <td> <input type="text" name="address"/> </td> </tr> <tr> <td>邮箱:</td> <td> <input type="email" name="email"/> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="注册"/> </td> </tr> </table> </form>
登录成功之后,我们跳转到message.jsp文件,读取出提示即可
message.jsp
<% String msg=(String)pageContext.findAttribute("msg"); if(msg==null){ return; } %> <%=msg %>
这样一个简单的注册登录系统就做完了。
阅读全文
0 0
- MVC模式实现(下)
- MVC模式实现(上)
- 目前正在学习mvc模式,转一篇java版本的snaker(运用mvc模式实现) 学习下
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- ASP.NET下MVC设计模式的实现
- 第三周周中总结
- oracle数据库基础
- Fiddler抓包手机APP数据,wireshark,charles
- Python09_进程与线程
- 应用层-4、Email应用
- MVC模式实现(下)
- PAT (Advanced) 1013. Battle Over Cities (25)
- poj3463 Sightseeing(读题很重要)
- Java-集合(list接口)
- 最小排序(美团点评)
- LeetCode #532 K-diff Pairs in an Array
- 51nod 1406 与查询
- 02-线性结构4 Pop Sequence(25 分)
- struts2命名空间namespace和访问路径的关系