JavaWeb---Servlet

MVC

Mmodel,模型功能JavaBean实现
Vview,视图用于展示,以及用户交互html,jsp,js,css,jquery等
Ccontrol,控制器接受各种请求,将请求跳转到模型,进行响应的处理,模型处理完毕之后,将结果返回给页面可以jsp实现,但是一般使用servlet实现控制器

servlet

什么是servlet:

    1.必须继承 Javax.servlet.http.HttpServlet
    2.必须重写其中的doGet()或者doPost()方法
        doGet(); 接受并处理所有get方式的请求
        doPost(); 接受并处理所有post方式的请求

代码演示(servlet2.5):

WelcomeServlet.java

package cn.edu;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class WelcomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println( "doget();" );
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	//一般get和post方法只用写一个
        //System.out.println( "dopost();" );
        this.doGet( req,resp );
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
        //利用servlet-name绑定相关的服务
    <servlet>
        <servlet-name>a</servlet-name>
        <servlet-class>cn.edu.WelcomeServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>a</servlet-name>
        <url-pattern>/WelcomeServlet</url-pattern>
        //注意这个地方的斜杠
    </servlet-mapping>
</web-app>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
      <a href="WelcomeServlet">welcomeservlet</a>
      <form action="WelcomeServlet" method="post">
          <input type="submit">
      </form>
  </body>
</html>

代码演示(servlet2.5):

3.0不需要修改web.xml

package cn.edu;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/WelcomeServlet")		//3.0的映射
public class WelcomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println( "servlet3.0" );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println( "dopost();" );
        this.doGet( req,resp );
    }
}

这里十分需要强调的一点就是@WebServlet("/WelcomeServlet")这里的/
这个地方的“/”十分重要,大家千万不要忘记了,不然改代码真的非常麻烦,有一次我改了一个通宵,就是因为这个斜杠

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值