美文网首页程序员
web开发日记-servlet

web开发日记-servlet

作者: 在南方的北方人_Elijah | 来源:发表于2017-02-19 22:31 被阅读101次

    servlet

    Servlet(Server Applet),全称Java Servlet,暂无中文译文。是用Java编写的服务器端程序。其主要功能在于交互式地浏览和修改数据,生成动态Web内容。狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。
    Servlet运行于支持Java的应用服务器中。从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器。
    最早支持Servlet标准的是JavaSoft的Java Web Server,此后,一些其它的基于Java的Web服务器开始支持标准的Servlet。

    servlet入门小实例

    src下创建com.webtest.servlet.HelloServlet.java

    package com.webtest.servlet;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    /**
     * Created by elijahliu on 2017/2/19.
     */
    public class HelloServlet extends javax.servlet.http.HttpServlet {
        protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    
        }
    
        @Override
        public void init(ServletConfig config) throws ServletException {
            System.out.println("==========init with parameter=============");
            super.init(config);
        }
    
        @Override
        public void init() throws ServletException {
            System.out.println("================init without parameter");
            super.init();
        }
    
        @Override
        public void destroy() {
    
            System.out.println("========destroy============");
            super.destroy();
        }
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            System.out.println("=============service===============");
            PrintWriter pw = resp.getWriter();//这三行用于将字符输出到浏览器,PrintWriter是一个字符输出流
            pw.println("Hellowworld");
            pw.close();//关闭输出流对象
        }
    
        protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
    
        }
    }
    
    

    这里继承了 httpservlet ,使用PrintWriter输出流输出到页面。

    相应的修改web.xml配置入口url

    <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <servlet-class>com.webtest.servlet.HelloServlet</servlet-class><!--servelt-class用于连接完全限定名,也就是name对应的类的路径-->
        </servlet>
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
            <url-pattern>/Hello</url-pattern><!--servlet mapping 通过name与servlet相连接,然后通过下面的url-pattern,当浏览器路径是这个的时候,访问这个路径,就会调用对应的servlet来处理-->
        </servlet-mapping>
    

    servelt-class用于连接完全限定名,也就是name对应的类的路径
    servlet mapping 通过name与servlet相连接,然后通过下面的url-pattern,当浏览器路径是这个的时候,访问这个路径,就会调用对应的servlet来处理

    servlet处理流程分析

    servlet处理流程

    处理流程:客户端发送request请求,到Tomcat webserver服务器,然后http服务器再再servlet容器中创建运行servlet,也就是说,在没有访问的请求发起时,servlet是不创建对象的。servlet的通过requset response对象来处理数据,结束后,可以再调用其他servlet,或最后直接将数据传输到浏览器返回响应。

    servlet执行流程

    servlet执行流程

    在创建servlet实例时,会调用init()方法来进行初始化,初始化后,servlet才会处于服务状态。整个servlet生命周期中,只能进行一次初始化。
    service()方法可以多次调用,这个请求次数有关,用来处理客户端的请求。
    destroy()方法,在servlet不再使用时,容器会调用destroy()方法进行销毁servlet实例。、

    servlet编程实例(1)

    实例流程

    首先创建Login.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>loginservlet</title>
    </head>
    <body>
    <form action="<%=request.getContextPath()%>/LoginServlet" method="post">
        <input type="text" name="uname"/>
        <input type="password" name="upwd"/>
        <input type="submit" value="submit">
    
    </form>
    
    </body>
    </html>
    

    其中的<%=request.getContextPath()%>
    request.getContextPath() 返回站点的根目录
    request.getRealpath("/")得到的是实际的物理路径,也就是你的项目所在服务器中的路径
    request.getScheme() 等到的是协议名称,默认是http
    request.getServerName() 得到的是在服务器的配置文件中配置的服务器名称 比如:localhost .baidu.com 等等
    request.getServerPort() 得到的是服务器的配置文件中配置的端口号 比如 8080等等

    再创建LoginServlet

    package com.webtest.servlet;
    
    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 java.io.IOException;
    
    /**
     * Created by elijahliu on 2017/2/19.
     */
    @WebServlet(name = "LoginServlet")
    public class LoginServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.service(req, resp);
    
            String username = req.getParameter("uname");
            String password = req.getParameter("upwd");
    
            System.out.println(username);
            System.out.println(password);
    
    
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    }
    
    

    最后在web.xml中更改servlet的路径配置

     <!--//loginservlet-->
        <servlet>
            <servlet-name>LoginServlet</servlet-name>
            <servlet-class>com.webtest.servlet.LoginServlet</servlet-class>
        </servlet>
        <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
            <url-pattern>/LoginServlet</url-pattern>
        </servlet-mapping>
    

    Servlet编程实例(2)

    使用doGet doPost方法来实现登录逻辑.

    get和post

    将service的业务逻辑放到doPost中,在doGet中调用doPost。降低代码的重复性,更符合逻辑。

    package com.webtest.servlet;
    
    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 java.io.IOException;
    
    /**
     * Created by elijahliu on 2017/2/19.
     */
    @WebServlet(name = "LoginServlet")
    public class LoginServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.service(req, resp);
    
            
    
    
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String username = request.getParameter("uname");
            String password = request.getParameter("upwd");
    
            System.out.println(username);
            System.out.println(password);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
    

    servlet编程实例(3) 添加登录后的处理流程

    流程

    建立success.jsp文件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>success</title>
    </head>
    <body>
    success
    用户<%=request.getParameter("uname")%><br />
    
    密码<%=request.getParameter("upwd")%><br />
    
    <a href="LoginServlet.jsp">back to login</a>
    
    </body>
    </html>
    
    

    建立error.jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>error</title>
    </head>
    <body>
    error happened
    用户<%=request.getParameter("uname")%><br/>
    
    密码<%=request.getParameter("upwd")%><br/>
    
    <a href="LoginServlet.jsp">back to login</a>
    </body>
    </html>
    

    更改LoginServlet文件,修改业务逻辑,重定向

    package com.webtest.servlet;
    
    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 java.io.IOException;
    
    /**
     * Created by elijahliu on 2017/2/19.
     */
    @WebServlet(name = "LoginServlet")
    public class LoginServlet extends HttpServlet {
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String username = request.getParameter("uname");
            String password = request.getParameter("upwd");
    
            System.out.println(username);
            System.out.println(password);
    
            if (username.equals("elijah") && password.equals("123456")) {//若用户密码相同,则重定向到相应的页面
                response.sendRedirect(request.getContextPath() + "/Servletpage/success.jsp");
            } else {
                response.sendRedirect(request.getContextPath() + "/Servletpage/error.jsp");
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
    

    Servlet编程实例(4)

    这里是用了请求转发,只进行一个请求响应流程。请求对象,传入servlet后,servlet将reques response对象转发到相应的成功失败页面。

    package com.webtest.servlet;
    
    import javax.servlet.RequestDispatcher;
    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 java.io.IOException;
    
    /**
     * Created by elijahliu on 2017/2/19.
     */
    @WebServlet(name = "LoginServlet")
    public class LoginServlet extends HttpServlet {
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String username = request.getParameter("uname");
            String password = request.getParameter("upwd");
    
            System.out.println(username);
            System.out.println(password);
    
            String forward = null;
            if (username.equals("elijah") && password.equals("123456")) {//若用户密码相同,则重定向到相应的页面
                forward = "/Servletpage/success.jsp";
                RequestDispatcher rd = request.getRequestDispatcher(forward);//requestDispatcher是一个请求转发对象
                //getRequestDispatcher获取一个Dispatcher对象,forward为转发的地址
                rd.forward(request, response);//执行请求转发,转发当前的额request 和response对象
    
                //请求重定向
                //response.sendRedirect(request.getContextPath() + "/Servletpage/success.jsp");
            } else {
    
                forward = "/Servletpage/error.jsp";
                RequestDispatcher rd = request.getRequestDispatcher(forward);//requestDispatcher是一个请求转发对象
                //getRequestDispatcher获取一个Dispatcher对象,forward为转发的地址
                rd.forward(request, response);//执行请求转发,转发当前的额request 和response对象
                //请求重定向
    
                //response.sendRedirect(request.getContextPath() + "/Servletpage/error.jsp");
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
    
    

    requestDispatcher是一个请求转发对象
    getRequestDispatcher获取一个Dispatcher对象,forward为转发的地址

    PS:需要注意一点的是,RequestDispatcher方法不能转发到不同的应用地址,如 转发地址为 www.google.com这是不可以的。但是response.sendRedirect重定向方法是可以的。

    Servlet编程实例(5)

    添加了 JBDC 数据库校验,完成校验用户的Service层的编写

    在DAO层添加实现获取用户的方法,返回结果集
    UserDao.java

    package com.webtest.dao;
    
    import com.webtest.entity.User;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    /**
     * Created by elijahliu on 2017/2/18.
     */
    public interface UserDao {
        public void save(Connection conn, User user) throws SQLException;
        public void update(Connection conn,Long id,User user) throws SQLException;
    
        public void delete(Connection conn, User user) throws SQLException;
    
        //需要编写相应的实现,这是典型的面向接口的编程,接口只定义外观和行为
    
        //获取用户信息
        public ResultSet get(Connection conn, User user)throws SQLException;
    
    }
    

    在UserDaoimpl中实现get方法

      @Override
        public ResultSet get(Connection conn, User user) throws SQLException {
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM tbl_user WHERE name=? AND password=?");
            ps.setString(1, user.getName());
            ps.setString(2,user.getPassword());
    
            return ps.executeQuery();
    
        }
    
    

    创建service层的包,com.webtest.service.

    package com.webtest.service;
    
    import com.webtest.dao.UserDao;
    import com.webtest.dao.impl.UserDaoimpl;
    import com.webtest.entity.User;
    import com.webtest.util.ConnectionFactory;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    /**
     * Created by elijahliu on 2017/2/19.
     */
    public class CheckuserService {
        private UserDao userDao= new UserDaoimpl();//上转型建立一个持久层类
        public boolean check(User user){
            Connection conn = null;
    
            conn = ConnectionFactory.getInstance().makeConnection();//获取一个连接工厂类的单例连接
            try {
                conn.setAutoCommit(false);//设置不自动提交
    
                ResultSet rs = userDao.get(conn, user);//获取查询结果集
                while (rs.next()) {//不为空,则存在,那么就返回true
                    return true;
                }
            } catch (SQLException e) {
                try {
                    conn.rollback();//错误发生则回滚
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }finally {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return false;
        }
    }
    

    Servlet编程实例(6)

    完成控制层的业务逻辑编写。在com.webtest.servlet下创建CheckServlet.java
    在这个servlet中调用service层的CheckServlet中的check()方法,完成用户的校验,与成功或失败页面的跳转。在web.xml中,完成servlet的路径配置。CheckServlet相当于控制层。
    CheckServlet.java

    package com.webtest.servlet;
    
    import com.webtest.entity.User;
    import com.webtest.service.CheckuserService;
    
    import javax.servlet.RequestDispatcher;
    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 java.io.IOException;
    
    /**
     * Created by elijahliu on 2017/2/19.
     */
    @WebServlet(name = "CheckServlet")
    public class CheckServlet extends HttpServlet {
    
        CheckuserService cku = new CheckuserService();//创建service 业务逻辑对象
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String uname = request.getParameter("uname");
            String passwd = request.getParameter("upwd");
    
            RequestDispatcher rd = null;
            String forward = null;
    
            if (uname == null && passwd == null) {//如果用户名或密码为空,则转发到错误页面
                request.setAttribute("msg", "用户名或密码为空");
                rd = request.getRequestDispatcher("/Servletpage/error.jsp");
                rd.forward(request,response);
            } else {
                User user = new User();//创建用户对象,使用service层的check方法 判断是否存在该用户
                user.setName(uname);
                user.setPassword(passwd);
                boolean bool = cku.check(user);//检查用户是否在数据库中存在
                if (bool) {
                    forward = "/Servletpage/success.jsp";
    
                }else{
                    request.setAttribute("msg","用户名或密码错误,重新输入");
                    forward = "/Servletpage/error.jsp";
                }
                rd = request.getRequestDispatcher(forward);//根据查出是否存在该用户,从而来确定执行转发的路径
                rd.forward(request,response);
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
    

    web.xml

    
    <!--checkservlet-->
        <servlet>
            <servlet-name>CheckServlet</servlet-name>
            <servlet-class>com.webtest.servlet.CheckServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>CheckServlet</servlet-name>
            <url-pattern>/CheckServlet</url-pattern>
        </servlet-mapping>
    

    Servlet编程实例(7)

    完成了前端页面的编写,运行了登录实例程序。登陆页面在finalservlet包中。
    对页面做一个最基础的页面美化。

    创建finalservlet目录 存放三个前端页面

    login.jsp

    <%--
      Created by IntelliJ IDEA.
      User: elijahliu
      Date: 2017/2/19
      Time: 20:28
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>login</title>
        <style type="text/css">
            body {
                color: #000;
                font-size: 14px;
                margin: 20px auto;
    
            }
    
        </style>
        <script type = "text/javascript">
            function check(form){
                if(document.forms.loginForm.uname.value == "") {
                    alert("请输入用户名!");
                    document.forms.loginForm.uname.focus();
                    return false;
                }
                if(document.forms.loginForm.upwd.value == "") {
                    alert("请输入密码!");
                    document.forms.loginForm.upwd.focus();
                    return false;
                }
    
            }
        </script>
    
    </head>
    <body>
    <form action="<%=request.getContextPath()%>/CheckServlet" method="post" name="loginForm">
        <table border="1" cellpadding="0" cellpdding="5" bordercolor="silver" align="center">
    
            <tr>
                <td colspan="2" align="center" bgcolor="#E8E8E8">用户登录</td>
            </tr>
            <tr>
                <td>用户名:</td>
                <td>
                    <input type="text" name="uname"/>
                </td>
            </tr>
            <tr>
                <td>密码:</td>
                <td>
                    <input type="password" name="upwd"/>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="submit" onclick="return check(this);"/><%--添加了一个onclik方法,触发时会调用check函数--%>
                </td>
            </tr>
        </table>
    
    </form>
    </body>
    </html>
    
    

    error.jsp

    <%--
      Created by IntelliJ IDEA.
      User: elijahliu
      Date: 2017/2/19
      Time: 16:16
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>error</title>
        <style type="text/css">
            body {
                color: #000;
                font-size: 14px;
                margin: 20px auto;
    
            }
            #message{
                test-align:center;
            }
    
        </style>
    </head>
    <body>
    <div id="message">
        error happened
        <%
            Object obj = request.getAttribute("msg");
            if (obj != null) {
                out.println(obj.toString());
            } else {
                out.println("none");
            }
        %>
    
        用户<%=request.getParameter("uname")%>
    <br/>
    
    密码<%=request.getParameter("upwd")%><br/>
    
    <a href="<%=request.getContextPath()%>/finalservlet/Login.jsp">back to login</a>
    </div>
    </body>
    </html>
    
    

    success.jsp

    <%--
      Created by IntelliJ IDEA.
      User: elijahliu
      Date: 2017/2/19
      Time: 16:14
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>success</title>
        <style type="text/css">
            body {
                color: #000;
                font-size: 14px;
                margin: 20px auto;
    
            }
    
            #message {
                test-align: center;
            }
    
        </style>
    </head>
    <body>
    <div id="message">
        success
        用户<%=request.getParameter("uname")%><br/>
    
        密码<%=request.getParameter("upwd")%><br/>
    
        <a href="<%=request.getContextPath()%>/finalservlet/Login.jsp">back to login</a>
    
    </div>
    </body>
    </html>
    
    

    相关文章

      网友评论

        本文标题:web开发日记-servlet

        本文链接:https://www.haomeiwen.com/subject/rdxjwttx.html