零基础学习java------33---------http协议,tomcat(其如何在eclipse上发布),注册案例

一. HTTP协议

  https://www.cnblogs.com/vamei/archive/2013/05/11/3069788.html

 

 

 

二. tomcat---------->web服务器

1. tomcat是一款提供web服务的软件,其用处为:

  (1)提供访问静态资源

  (2)给用户提供了自定义业务逻辑的接口

2. 什么是web服务器?

  遵循http协议,可以提供访问静态资源和动态资源的软件称为web'服务器()

浏览器请求服务器的流程图

 

 

 3. tomcat软件的目录结构

 

 

 

 

 

 startup.bat   双击启动

4. tomcat在eclipse发布

第一步:点击eclipse上的window--->Preferences--->Server--->runtime Enviroments---->Add,得到如下图

注意,此处可能会没有Server选项,解决办法-------->https://www.cnblogs.com/jj1106/p/11599239.html

 

 

 选择自己的tomcat版本

 

 

 

 

 点击apply and  close即可完成发布

 

 点击eclipse右上角的open Perspective,并在下拉框中选择java EE,如下图

注意,此处可能没有java EE的选项(没安装java ee的插件),解决办法:https://www.cnblogs.com/jj1106/p/11600948.html

 

 

这时候,下面就会出现Servers选项(若没有的话windows --->  show view - >  servers),如下图

 

 

 

 

 

 

 

 5. 创建web项目

(1)new   --->  dynamic web project -- >

 

 

 

 

(2)将项目部署在tomcat中

 

 

 

 

 (2)部署完项目后就启动项目

 

 访问此web服务器:http://localhost:8080/day12/demo1.html

即可得到demo1.html中的文本内容

 

 

三. 注册案例

1. 前端注册页面(写在WebCont中):

访问时:localhost:8080/项目名/reister.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
    <h1>xxx注册中心</h1>
    <form action="Register" method="post">
    姓名:<input type="text" name="username" /></br></br>
    昵称:<input type="text" name="nickname" /></br></br>
    密码:<input type="password" name="password" /></br></br>
    手机号:<input type="text" name="phonenumber" /></br></br>
    邮箱:<input type="text" name="mail" /></br></br>
    <input type="submit" value="注册"/>
    </form>>
</body>
</html>
View Code

2. 后台部分:

 

 

 (1)admin

Register

创建的时候是选择servlet

public class Register extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    UserService userService = new UserServiceImpl();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        try {
            // 获取前台发送的参数
            String username = request.getParameter("username");
            String nickname = request.getParameter("nickname");
            String password = request.getParameter("password");
            String phonenumber = request.getParameter("phonenumber");
            String mail = request.getParameter("mail");
            System.out.println(username);
            // User对象去接受这些参数
            User user = new User(username, nickname, password, phonenumber, mail);
            boolean b = userService.register(user);
            if(b) {
                response.sendRedirect("/day12/success.html");
            }else {
                response.sendRedirect("/day12/register.html");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}
View Code

(2)service

UserService接口

public interface UserService {
    
    // 注册
    public boolean register(User user) throws Exception;
}
View Code

UserService接口实现类(UserServiceImpl)

public class UserServiceImpl implements UserService{
    
    /**
     * 注册实现
     */
    @Override
    public boolean register(User user) throws Exception {
        boolean flag = false;
        UserDao userDao = new UserDaoImpl();
        int i = userDao.insertUserToDb(user);
        if(i>0) {
            flag = true;
        }
        return flag;
    }
}
View Code

(3)pojo

User(根据从前端接收的数据来确定属性)

public class User {
    private String username;
    private String nickname;
    private String password;
    private String phonenumber;
    private String email;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getPhonenumber() {
        return phonenumber;
    }
    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public User(String username, String nickname, String password, String phonenumber, String email) {
        super();
        this.username = username;
        this.nickname = nickname;
        this.password = password;
        this.phonenumber = phonenumber;
        this.email = email;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", nickname=" + nickname + ", password=" + password + ", phonenumber="
                + phonenumber + ", email=" + email + "]";
    }
    
}
View Code

(4)dao

此处需要导一些jar包,如下(不需要build path)

 

 UserDao接口

public interface UserDao {
    
    
    /**
     * 往数据库中添加用户
     * @param user
     * @return
     */
    public int insertUserToDb(User user) throws Exception;
}
View Code

UserDao实现类

public class UserDaoImpl implements UserDao{
    static QueryRunner runner;
    static {
        // 获取数据库池对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        runner = new QueryRunner(dataSource);
    }
    @Override
    public int insertUserToDb(User user) {
        
        int in = 0;
        try {
            String sql = "insert into user values(null,?,?,?,?,?)";
            in = runner.update(sql,user.getUsername(),user.getNickname(),user.getPassword(),user.getPhonenumber(),user.getEmail());
        } catch (Exception e) {
            in = -1;
        }
        return in;
    }
}
View Code

 

posted @ 2019-09-27 23:18  一y样  阅读(243)  评论(0编辑  收藏  举报