赞
踩
关键字:implement,extends,private等
异常处理:try{...}catch(Exception e){...}
基础语法:遍历,循环,正则表达式,javaweb的基础
重点(自认为):通过form表单,实现与servlet的连接,servlet方法实现
- <form action="LoginServlet?method=login" method="post"></form>
- action出错/漏写 method="post"都会造成报错
- body{
- /**
- 引入背景图片,设置尺寸
- */
- background: url(img/eat.gif);
- background-size: 800px 900px;
-
- }
- span{
- /*文本居中
-
- */
- text-align: center;
- font-size: 30px;
- }
-
- <body>
-
- <span><h1 >吃货联盟</h1></span>
- <a href="user/login.jsp"><h2>登录</h2></a>
- <a href="user/regist.jsp"><h2>注册</h2></a>
- <a href="user/re.jsp"><h3>用户使用</h3></a>
- <a href="manager/ch.jsp"><h3>商家后台管理</h3></a>
- <br/>
- <form action="<%=request.getContextPath()%>/logoutServlet" method = "post">
- <input type="submit" value="退出系统"/>
- </form>
- <br/>
-
- </body>
- private int id;
- private String name;
- private String pwd;
- private String sex;
- private String home;
- private String tel;
- public class UserDaoimpl extends BaseDao<User> implements UserDao{
-
- @Override
- public User getUserByUserNameAndPassword(User user) {
- String sql="select * from user where name=? and pwd=?";
- User bean=this.getBean(sql, user.getName(),user.getPwd());
- return bean;
-
- }
-
- @Override
- public boolean registUser(User user) {
- String sql="insert into user(name,pwd) values(?,?)";
- //update>0成功
- int update=this.update(sql, user.getName(),user.getPwd());
- return update>0;
- }
-
- @Override
- public User getUserByUserName(User user) {
- String sql="select * from user where username=?";
- return getBean(sql,user.getName());
- }
-
- }
- public class UserServiceImpl implements UserService {
- private UserDao ud=new UserDaoimpl();
- @Override
- public User login(User user) {
- return ud.getUserByUserNameAndPassword(user);
- }
-
- @Override
- public boolean regist(User user) {
- return ud.registUser(user);
- }
-
- @Override
- public boolean checkUser(User user) {
- User byUserName=ud.getUserByUserName(user);
- return byUserName==null;
-
- }
-
- }
- private int id;
- private String name;
- private double price;
- private int sales;//销量
- public class DishDaoImpl extends BaseDao<Dish> implements DishDao{
-
- @Override
- public List<Dish> getAllDishs() {
- String sql="select id,name,price,sales from dish";
- //返回所有的图书
- return getBeanList(sql);
- }
-
- @Override
- public boolean addDish(Dish dish) {
- // TODO Auto-generated method stub
- String sql="insert into dish(id,name,price,sales) values(?,?,?,?)";
- int update=update(sql,dish.getId(),dish.getName(),dish.getPrice(),dish.getSales());
- //影响了多少行
- return update>0;
- }
-
- @Override
- public boolean delDish(Dish dish) {
- String sql="delete from dish where id=?";
- int update =update(sql, dish.getId());
- return update>0;
- }
-
- @Override
- public boolean updateDish(Dish dish) {
- String sql="update dish set name=?,price=?,sales=? where id=?";
- int update=update(sql,dish.getName(),dish.getPrice(),dish.getSales(),dish.getId());
- //影响了多少行
- return update>0;
- }
-
- @Override
- public Dish getDish(Dish dish) {
- String sql="select id,name,price,sales from dish where id=?";
- return getBean(sql,dish.getId());
- // return getBean(sql,dish.getId());)
- }
-
-
-
- @Override
- public int getTotalCount() {
- String sql="select count(*) from dish";
- Object object=getSingleValue(sql);
- int parseInt=0;
- try {
- parseInt=Integer.parseInt(object.toString());
- } catch (NumberFormatException e) {
- e.printStackTrace();
- }
- //)
- return parseInt;
- }
-
- @Override
- public int getTotalCountByPrice(double minPrice, double maxPrice) {
- String sql="select count(*) from dish where price between ? and ?";
- Object object=getSingleValue(sql, minPrice,maxPrice);
- int i=0;
- try {
- i=Integer.parseInt(object.toString());
- } catch (NumberFormatException e) {
- e.printStackTrace();
- }
- return i;
- }
-
- @Override
- public List<Dish> getPageByPrice(int index, int size, double minPrice, double maxPrice) {
- String sql="select id,name,price,sales from dish between ? and ? limit ?,?";
- return getBeanList(sql,minPrice,maxPrice,index,size);
- }
-
- }
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title here</title>
- <style type="text/css">
- body{
- background: url(../img/login.gif);
- background-size: 1200px 700px;
-
- }
- h3{
- color:teal;
- }
- </style>
- </head>
- <body>
- <h3>${msg }</h3>
- <form name="form1" action="LoginServlet?method=login" method="post">
- <table width="200" border="3">
- <tr>
- <td colspan="2"><h1>欢迎登录</h1></td>
- </tr>
- <tr>
- <td>用户名</td>
- <td><input type="text" name="username" size="10"></td>
- </tr>
- <tr>
- <td>密码</td>
- <td><input type="password" name="password" size="10"></td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" name="submit" value="登录"> <a
- href="regist.jsp">注册新用户</a></td>
- </tr>
- </table>
- </form>
-
- </body>
- </html>
- package servlet;
-
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import impl.UserServiceImpl;
- import user.User;
- import user.UserService;
-
- @WebServlet("/LoginServlet")
-
-
- public class LoginServlet extends BaseServlet {
- private static final long serialVersionUID = 1L;
- private UserService us=new UserServiceImpl();
-
- protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html;charset=utf-8");
- String username=request.getParameter("username");
- String password=request.getParameter("password");
- User user=us.login(new User(0, username, password, null, null,null));
- if(user==null) {
- //登陆失败 返回登陆页面 转发
- request.setAttribute("msg", "用户名密码错误");;
- request.getRequestDispatcher("/user/login.jsp").forward(request, response);
- }else {
- //登陆成功 返回成功页面 重定向
- response.sendRedirect(request.getContextPath()+"/user/login_success.jsp");
- }
- }
-
- }
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Insert title here</title>
- <style type="text/css">
-
- .form{
- height: 420px;
- margin-top: 25px;
- margin: 25px auto 0;
- display: flex;
- justify-content: center;
- align-items: center;
-
- }
- span{
- /*文本居中
-
- */
- text-align: center;
- font-size: 30px;
- }
- body{
- background: url(../img/er2.gif);
- background-size: 1200px 700px;
-
- }
- .errorMsg{
- color: red;
- }
- </style>
- <script src="../script/jquery-1.12.4.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function(){
- $("#sub_tn").click(function(){
- // 验证用户名:必须由字母,数字下划线组成,并且长度为5到12位
- //1 获取用户名输入框里的内容
- var usernameText = $("#username").val();
- //2 创建正则表达式对象
- var usernamePatt = /^\w{5,12}$/;
- //3 使用test方法验证
- if (!usernamePatt.test(usernameText)) {
- //4 提示用户结果
- $("span.errorMsg").text("用户名不合法!");
-
- return false;
- }
- // 验证密码:必须由字母,数字下划线组成,并且长度为5到12位
- //1 获取用户名输入框里的内容
- var passwordText = $("#password").val();
- //2 创建正则表达式对象
- var passwordPatt = /^\w{5,12}$/;
- //3 使用test方法验证
- if (!passwordPatt.test(passwordText)) {
- //4 提示用户结果
- $("span.errorMsg").text("密码不合法!");
-
- return false;
- }
- // 验证确认密码:和密码相同
- //1 获取确认密码内容
- var repwdText = $("#repwd").val();
- //2 和密码相比较
- if (repwdText != passwordText) {
- //3 提示用户
- $("span.errorMsg").text("确认的密码和密码不一致!");
-
- return false;
- }
- //验证手机号是否正确
- var telText=$("#tel").val();
- var telPatt = /^(86)?((13\d{9})|(15[0,1,2,3,5,6,7,8,9]\d{8})|(18[0,5,6,7,8,9]\d{8}))$/;
- if(!telPatt.test(telText)){
- $("span.errorMsg").text("手机号码格式错误!");
- return false;
- }
-
-
- // 验证码:现在只需要验证用户已输入。因为还没讲到服务器。验证码生成。
- var codeText = $("#code").val();
-
- //去掉验证码前后空格
- // alert("去空格前:["+codeText+"]")
- codeText = $.trim(codeText);
- // alert("去空格后:["+codeText+"]")
-
- if (codeText == null || codeText == "") {
- //4 提示用户
- $("span.errorMsg").text("验证码不能为空!");
-
- return false;
- }
-
- // 去掉错误信息
- $("span.errorMsg").text("");
-
- })
- })
- </script>
- </head>
- <body>
-
- <div>
- <div id="l_content">
- <span class="login_word"><h1>欢迎注册</h1></span>
- <span class="errorMsg" >
- <h3>${msg==null?"请输入用户名":msg}</h3>
- </span>
- </div>
-
- <div class="form">
- <form action="RegistServlet?method=regist" method="post">
- 用户名称:
- <input name="username" type="text" id="username" placeholder="请输入用户名">(由字母,数字下划线组成,并且长度为5到12位)<br/>
- <br/>
- 用户密码:
- <input name="password" type="password" id="password" placeholder="请输入密码">(由字母,数字下划线组成,并且长度为5到12位)<br/>
- <br/>
- 确认密码:
- <input name="repwd" type="password" id="repwd" placeholder="请再次输入密码"><br/>
- <br/>
- 选择性别:
- <input id="man" type="radio" name="sex" value="男" checked>男
- <input type="radio" id="women"name="sex" value="女" checked>女<br/>
- <br/>
- <label>用户地址:</label>
- <textarea rows="1" cols="17" placeholder="请输入地址"></textarea>
- <br/>
- <br/>
- 电话号码:
- <input name="tel" rows="1" cols="10" id="tel" placeholder="请输入电话号码"/>
- <br/>
- <br/>
- <td>
- <tr><label> 验证码:   </label></tr>
- <tr><input type="text" name="code" id="code" style="width:90px;height: 35px " placeholder="请输入验证码"/> </tr>
-
- <tr><img alt="" src="../img/code.bmp" style=" width:100px;height:40px;position: absolute" id="codeImg"></tr>
- </td>
- <br/>
- <br/>
- <input id="sub_tn" type="submit" value="注册" style="width: 60px;height: 30px">
- <input id="reset" type="reset" value="重置" style="width: 60px;height: 30px">
- </form>
- </div>
- </div>
-
-
- </body>
- </html>
- package servlet;
-
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import impl.UserServiceImpl;
- import user.User;
- import user.UserService;
-
- @WebServlet("/RegistServlet")
-
- public class RegistServlet extends BaseServlet {
- private static final long serialVersionUID = 1L;
- private UserService us=new UserServiceImpl();
-
-
- protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html;charset=utf-8");
- String username=request.getParameter("username");
- String password=request.getParameter("password");
- boolean b=us.regist(new User(0, username, password,null, null, null));
-
- if(b) {
- //注册成功 返回成功页面 重定向
- response.sendRedirect(request.getContextPath()+"/user/regist_success.jsp");
- }else {
- //注册失败 返回注册页面 转发
- request.setAttribute("msg", "该用户已存在");
- request.getRequestDispatcher("/user/regist.jsp").forward(request, response);
-
- }
- }
-
- }
- public class LoginFilter implements Filter {
-
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
- throws IOException, ServletException {
- System.out.println("filter已经启动");
- //验证用户是否登录,如果登录,放行,否则转到登陆页面
- HttpServletRequest req=(HttpServletRequest) request;
- HttpServletResponse res = (HttpServletResponse)response;
- /*String urlString=req.getRequestURI();
- if(urlString.endsWith("login.jsp")||urlString.endsWith("LoginServlet")) {
- chain.doFilter(request, response);
- return;
- }*/
- //获取已经登陆的用户
- User user=Webutils.getLoginUser(req);
- if(user==null) {
- request.setAttribute("msg", "此操作需要登录,请先登录");
- // req.getRequestDispatcher("/user/login.jsp").forward(request, response);
- res.sendRedirect(req.getContextPath()+"/user/login.jsp");
- }else {
- chain.doFilter(request, response);
- }
-
- }
-
- @Override
- public void destroy() {
- // TODO Auto-generated method stub
-
- }
-
- }
- <filter>
- <filter-name>LoginFilter</filter-name>
- <filter-class>filter.LoginFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>LoginFilter</filter-name>
- <url-pattern>(要拦截的)</url-pattern>
-
- </filter-mapping>
请联系博主
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。