Servlet案例二

右键 open Type Hierarchy

OPTIONS用于返回服务器支持的 HTTP Method

POST用于将指定的资源提交到服务器进行处理

GET用于从指定的资源请求数据

PUT用于上传指定资源

DELETE用于删除指定的资源

HEAD与GET方法相同,但是返回的只是HTTP的包头,不会返回文档的主体,一般在浏览器中输入网址访问资源都是通过GET方法;

在表单提交的时候可以通过method属性指定提交方式是get或者post,默认的提交方式是。



修改LoginServlet.java文件为:

package com.jikexueyuan.servlet;


import java.io.IOException;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public classLoginServlet extendsHttpServlet {


//点击 public class LoginServlet extends HttpServlet左边的警告 按钮  点击Add generated serial version ID 生成以下一行代码:序列号版本ID,用于序列化和反序列化

/**

*/

private staticfinal long serialVersionUID = 6520550639750164887L;


//@Override

// protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//         String userName = req.getParameter("uname");

//         String password = req.getParameter("upwd");

//         

//         System.out.println("用户名 ==》 " + userName);

//         System.out.println("密码 ==》 " + password);

// }


@Override

protectedvoid doGet(HttpServletRequestreq, HttpServletResponse resp)throws ServletException,IOException {

System.out.println("======进入doGet方法======");

        String userName =req.getParameter("uname");

        String password =req.getParameter("upwd");

        

        System.out.println("用户名 ==》 "+ userName);

        System.out.println("密码 ==》 "+ password);

}


@Override

protectedvoid doPost(HttpServletRequestreq, HttpServletResponse resp)throws ServletException,IOException {

System.out.println("======进入doPost方法======");

        String userName =req.getParameter("uname");

        String password =req.getParameter("upwd");

        

        System.out.println("用户名 ==》 "+ userName);

        System.out.println("密码 ==》 "+ password);

}


}






打开浏览器访问


 login.jsp方法为post

eclipse控制台

======进入doPost方法======

用户名 ==》 developer

密码 ==》 111111



修改 login.jsp方法为get

eclipse控制台

======进入doGet方法======

用户名 ==》 developer

密码 ==》 111111

浏览器会看到输入的用户名和密码

http://localhost:8080/JSPStudy/loginServlet?uname=developer&upwd=111111&Login=%E6%8F%90%E4%BA%A4




public   class   LoginServlet   extends   HttpServlet   按住command键点击 HttpServlet

HttpServlet.class  Window-Show View -Outline 找到 service方法
优化代码 

@Override

protectedvoid doGet(HttpServletRequestreq, HttpServletResponse resp)throws ServletException,IOException {

System.out.println("======进入doGet方法======");

doPost(req,resp);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值