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方法的区别:
* doget:
* 处理get请求方式
* dopost:
* 处理post请求方式
*
* Service:
* 优先处理,
*
* 注意:
* 如果在覆写的service方法中调用了父类的service方法(super.service(arg0, arg1);
* 则在service方法处理完成时会在次根据请求方式调用doget和dopost方法
* 一般我们不在覆写service中调用父类的方法,避免出现405错误。
* Servlet的常见错误
* 404错误:资源未找到
* 原因一:在请求地址中的servlet别名书写错误
* 原因二:虚拟项目名称书写错误
* 500错误: 内部服务器错误
* 错误一 <servlet-class>com.sxt.in.ServletMthod</servlet-class>
* 解决
* 在web.xml中校检servlet类的全限路径是否拼写 错误.
* 错误二
* 因为service方法体中的代码执行错误
*
* 解决:根据错误提示对代码进行更改
* 405错误:请求方式不支持
* 原因:
* 请求方式和service中的方法不匹配所造成的的
* 解决:
* 尽量使用service方法处理请求,并且在service方法中不要调用父类的service方法
* @author Administrator
* Ctrl+F 查找别名
*/
public class ServletMethod extends HttpServlet {
// @Override
// protected void service(HttpServletRequest req, HttpServletResponse resp)
// throws ServletException, IOException {
// System.out.println("我是service");
//
// }
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(arg0, arg1);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("我说dogetservice");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("我是dopost方法");
} }
ServeletMethod.java以上
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'MethodJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body> <form action="method" method="get">
用户名:<input type="text" name="uname" value=""><br />
密码:<input type="password" name="pwd" value=""><br />
<input type="submit" value="登录"> </form>
</body>
</html>
jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>servletlife</servlet-name>
<servlet-class>com.sxt.in.servletlife</servlet-class>
<load-on-startup>1</load-on-startup><!-- 加载服务器启动流 其中的数字1表示项目的加载顺序-->
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletMethod</servlet-name>
<servlet-class>com.sxt.in.ServletMethod</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>servletlife</servlet-name>
<url-pattern>/life</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletMethod</servlet-name>
<url-pattern>/method</url-pattern>
</servlet-mapping> </web-app>
xml代码
Servlet的Service方法和doget 和 dopost方法的区别,常见的错误解析的更多相关文章
- service 方法和doGet、doPost方法的区别
Service方法和doGet和doPost方法的区别service: 可以处理get/post方式的请求,如果servlet 中有service方法,会优先调用service方法进行处理do ...
- Servlet的doGet与doPost方法的区别与使用
Servlet的doGet与doPost方法的区别与使用 2016年07月07日 13:05:13 阅读数:10222 一,区别 在使用表单提交数据到服务器的时候有两张方式可共选择,一个是post一个 ...
- toString方法和valueof()方法的区别
JavaScript引用类型之Array数组的toString()和valueof()方法的区别 一.转换方法 1.在JavaScript中几乎所有对象都具有toLocaleString().to ...
- Thread start()方法和run()方法的区别
转自:http://www.cnblogs.com/skywang12345/p/3479083.html start():作用一个新的线程,新线程会执行相应的run()方法,start()不能被重复 ...
- 牛客网Java刷题知识点之调用线程类的start()方法和run()方法的区别
不多说,直接上干货! 前期博客 牛客网Java刷题知识点之四种不同的方式创建线程 这里很简单 首先,系统通过调用线程类的start()方法来启动一个线程,此时这个线程处于就绪状态,而非运行状态,也就意 ...
- [转]servlet中的service, doGet, doPost方法的区别和联系
原文地址:http://m.blog.csdn.net/blog/ghyg525/22928567 大家都知道在javax.servlet.Servlet接口中只有init, service, des ...
- 自定义servlet重写doGet或doPost方法是如何实现多态的
我们知道,如果我们自定义一个servlet继承HttpServlet,并且重写HttpServlet中的doGet或doPost方法,那么从浏览器发送过来的request请求将调用HttpServle ...
- 去除myeclipse中doget和dopost方法中的注释
当我们使用myeclipse新建servlet时发现doget和dopost方法中有一些无用的注释,每次新建一个servlet时都要手动删除特别麻烦. 下面就教大家如何去除这些注释! 以myeclip ...
- Ext.Ajax.request()方法和FormPanel.getForm().submit()方法,都返回success()方法的差异
我还是不发表到博客园首页吧,要不然还是要被取消,>_< 还是言归正传吧,关于Ext.Ajax.request()方法和FormPanel.getForm().submit()方法返回suc ...
随机推荐
- 在Servlet中使用@Autowire的方法
在你调用的Servlet中添加如下代码: public void init(ServletConfig config) { try { super.init(config); SpringBeanAu ...
- subprocess使用小方法
import subprocess def create_process(cmd): p = subprocess.Popen(cmd, shell=True, stdout=subp ...
- 关于在Qt里让程序休眠一段时间的方法总结
出处:http://hanzhaoxin.cnblogs.com/ Qt 为何没有提供 Sleep 论坛上不时见到有人问: Qt 为什么没有提供跨平台的 sleep 函数? 使用平台相关的 Sleep ...
- 激活函数:Swish: a Self-Gated Activation Function
今天看到google brain 关于激活函数在2017年提出了一个新的Swish 激活函数. 叫swish,地址:https://arxiv.org/abs/1710.05941v1 pytorch ...
- django 模板中{%for%}的使用
1.{%for athlete in list reversed%} reversed用于反向迭代 2.for 标签 支持一个可选的 empty 变量 3.forloop 模板变量 4.forloo ...
- Vue之computed与watch的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Sqlserver查询结果集插入新表
数据库“Test” 数据库“Test2” 表 “fromTable” 表 “toTable” 表 “newTable” 字段 “name”,“age”,“gender” 原因:公司有2个数据库,一个是 ...
- 安装Windows10+Ubentu18双系统
1.先安装Windows系统,安装完成后,使用磁盘管理工具划分出一定的freespace空间留给linux安装系统用. 2.使用Universal-USB-Installer制作ubentu启动U盘. ...
- Python中的函数(5)
一.向函数中传递任意数量的实参 有时候,你预先不知道函数需要接受多少个实参,Python中函数可以收集任意数量的实参. 栗子:来看一个打印好友列表功能的函数,它需要接收任意数量的好友名.如下: def ...
- 404 Not Found 由来
404 NOT FOUND! 抱歉,沒有找到您需要的文章!! 什么是 404 Not Found 404页面是网站必备的一个页面,它承载着用户体验与SEO优化的重任.404页面通常为用户访问了网站上不 ...