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 ...
随机推荐
- UVA 11346 Probability 概率 (连续概率)
题意:给出a和b,表示在直角坐标系上的x=[-a,a] 和 y=[-b,b]的这样一块矩形区域.给出一个数s,问在矩形内随机选择一个点p=(x,y),则(0.0)和p点组成的矩形面积大于s的概率是多少 ...
- 遍历NSView下的子视图方法
如何遍历NSView下的子视图呢 for (NSView *aview in [SuperV subviews]) { if([aview isMemberOfClass:[NSButton clas ...
- [Redis] 基于redis的分布式锁
前言分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁. 可靠性首先,为了确保 ...
- 路由器wan口ip地址显示0.0.0.0怎么办
http://m.xuexila.com/luyouqi/671049.html 这个网络时代里面我们最常用来连接网络的设备就是路由器了,现在的社会不管是工作还是生活几乎都离不开网络了,同时我们也要学 ...
- SFM作业
代码:https://github.com/jianxiongxiao/SFMedu PPT:http://3dvision.princeton.edu/courses/SFMedu/slides.p ...
- Linux-01 虚拟机Linux的安装
学习要点 虚拟机VMware Workstation 11.0 CentOS6.5的安装 Linux简介 Linux 一种免费开源的操作系统 常作为服务器的操作系统使用 基本思想:一切都是文件 常用发 ...
- Ubuntu 18的网络配置
包括Ubuntu 18.04和18.10,设置为静态IP及DNS. sudo vim /etc/netplan/50-cloud-init.yaml network: ethernets: enp4s ...
- source 1.7 中不支持 lambda 表达式(请使用 -source 8 或更高版本以启用 lambda 表达式)
from:http://blog.csdn.net/qwdafedv/article/details/54691740 https://www.youtube.com/watch?v=oXxijdf8 ...
- Maven实战读书笔记(一):Maven概述
1.1 Maven是什么,能做什么 Maven是一个跨平台的项目管理工具,主要服务于Java平台的项目构建.依赖管理和项目信息管理. Maven的用途之一是项目构建,能够自动化构建过程,从清理.编译. ...
- 拖拽功能-jquery
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...