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 ...
随机推荐
- 联玛客(W 笔试)
纸质算法题 1. 输入数据:1.3.2.4.8... 输出数据:3.1.4.2.8... 找出规律,写出一个程序求解,并附上时间复杂度和空间复杂度 我的答案: 规律一:奇偶位互换 假设输入数据长度为5 ...
- DROP USER - 删除一个数据库用户帐号
SYNOPSIS DROP USER name DESCRIPTION 描述 DROP USER 从数据库中删除指定的用户. 它不删除数据库里此用户所有的表,视图或其他对象. 如果该用户拥有任何数据库 ...
- ubuntu卡机
卡机了用ctrl+alt+t打开终端然后top看后台程序 最后kill -9 + PID就能把最影响问题的程序杀掉 我之前就杀了一个占100%cpu的程序
- MFC_综述
第一天(win消息机制.SDK编程基础) 1.基本概念介绍(SDK.API.句柄.消息队列.winmain函数) 2.第一个Windows界面程序(winAPI) 3.消息循环和窗口 ...
- 连接MySQL错误“plugin caching_sha2_password could not be loaded”的解决办法
MySQL新版默认使用caching_sha2_password作为身份验证插件,而旧版是使用mysql_native_password.当连接MySQL时报错“plugin caching_sha2 ...
- 工作流activi链接地址
http://topmanopensource.iteye.com/blog/1313865
- Python中的类(2)
一.使用类和实例 我们先编写一个学生的类,它存储了有关学生的信息,还有一个整合学生信息的方法: student.py class Student(): def __init__(self,name,a ...
- 访问变量的每个字节内容(c语言)
#include <stdio.h> #define fun(x) for(int fun_i = 0; fun_i < sizeof(x); fun_i++){printf(&qu ...
- Fiddler抓包-工具介绍(request和response)
from:https://www.cnblogs.com/yoyoketang/p/6731121.html 本篇简单的介绍下fiddler界面的几块区域,以及各自区域到底是干什么用的,以便于各好的掌 ...
- maven+struts2环境搭建
首先在struts2.xml文件配置一个包,在包中配置一个action,新建action,新建视图,在action中定义由method定义的方法,这个方法一定要返回String类型,返回的是视图的名称 ...