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代码
原文地址:https://www.cnblogs.com/dream2060/p/10925437.html
时间: 2024-10-15 12:05:18