在servlet中出现service()、doGet()和doPost()方法时的执行问题

在建立一个继承了HttpServlet类并重写了该类的service()、doPost()和doGet()方法时,java会如何执行?其实若是该三个方法都在存在的情况下,java只会执行service()方法,而其他的两种方法不会被执行。若是没有service() 方法,则是根据jsp传入方式选择对应的方法。比如说,若是jsp是以Post方式传入数据,则是调用doPost()方法处理数据。但是一般上在建立一个继承HttpServlet类时都会重写doPost()和doGet()方法,而且会在其中一个方法中处理数据,另一个方法则是直接调用该方法,比如以下例子:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // 请求编码方式(设置服务器端编码方式)
        request.setCharacterEncoding("utf-8");
        // 响应编码方式(设置浏览器端发送编码方式)
        response.setContentType("text/html; charset=utf-8");        
        String name = request.getParameter("name");
        String psw = request.getParameter("psw");
        if ("admin".equals(name) && "admin".equals(psw))
        {
            response.getWriter().append("欢迎" + name + "登录本页面");
        }
        else
        {
            PrintWriter out = response.getWriter();
            out.print("<script>location.href=‘faild.jsp‘</script>");
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }

以上仅是个人见解,如有失误,欢迎指出 !^^!

时间: 2024-08-10 02:11:15

在servlet中出现service()、doGet()和doPost()方法时的执行问题的相关文章

转: servlet中的service, doGet, doPost方法的区别和联系

大家都知道在javax.servlet.Servlet接口中只有init, service, destroy方法 但是我们在继承HttpServlet的时候为何一般重写doGet和doPost方法呢 下面我们看一下Servlet源代码: 注意: JDK只是定义了servlet接口,而实现servlet接口的比如tomcat, jboss等服务器 下面我们找到tomcat源代码, 可以到官网下载, 目前最新版本是7.0.53 找到javax.servlet.http.HttpServlet类, 下

Servlet之doGet()和doPost()方法区别和联系【入门版,初学者必看】

Servlet 中 重写的doGet(),doPost()方法,分别代表get请求和post请求 其实不管get请求,还是post请求,处理方式类似 所以一般习惯在doGet()中调用一下doPost ()或者 在doPost()里面调用doGet() 版权声明:本文为博主原创文章,未经博主允许不得转载.

Servlet的Service方法和doget 和 dopost方法的区别,常见的错误解析

package com.sxt.in; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Service方法和doget 和 dopost方

Servlet的doGet与doPost方法的区别与使用

Servlet的doGet与doPost方法的区别与使用 2016年07月07日 13:05:13 阅读数:10222 一,区别 在使用表单提交数据到服务器的时候有两张方式可共选择,一个是post一个是get.可在<form>中的method属性中指定提交的方式.如:<form action="inputForm"method="get">,如果不指定method属性,则会默认该属性为"get"方式. Get和post都能

jsp及servlet中获取项目路径的一些方法

获取项目的路径:1.在实现了servlet接口的实现类中:根据config 调用方法,config.getServletContext().getContextPath(); 2.在一个直接创建的servlet类中(实际上是实现了httpservlet):request.getContextPath(); 3.在jsp中:由九大内置对象的request对象获取.request.getContextPath(); 4.在EL标签中${pageContext.request.contextPath}

解决spring boot中普通类中使用service为null 的方法

我使用的是springboot+mybatisplus +mysql1.创建一个SpringUtil工具类 import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public final class SpringUtil im

UIWebView中Html中用JS调用OC方法及OC执行JS代码

1.HTML页面 1 <html> 2 3 <head> 4 5 <title>HTML中用JS调用OC方法</title> 6 7 <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> 8 9 <script> 10 11 function test() 12 13 { 14 15 alert("test

servlet中service doGet doPost 的关系

在servlet中默认情况下,无论你是get还是post 提交过来都会经过service()方法来处理,然后转向到doGet 或是doPost方法,可以看HttpServlet 类的service方法: protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); if(

servlet中service() doGet() doPost() 方法

HttpServlet 里的三个方法:service(HttpServletRequest req, HttpServletResponse resp) ,doGet(HttpServletRequest req, HttpServletResponse resp), doPost(HttpServletRequest req, HttpServletResponse res)的区别和联系: 在servlet中默认情况下,无论你是get还是post 提交过来 都会经过service()方法来处理