Java 基础篇 Servlet 基础用法--Get

 

Get : 链接包含报文内容

GET 方法向页面请求发送已编码的用户信息。页面和已编码的信息中间用 ? 字符分隔,如下所示:

http://127.0.0.1:8080/Servlets/ReidDemo1?name=Reid&url=http://127.0.0.1:8080/Servlets/ReidDemo1

代码示例:

package com.reid.Demo1;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class RiedDemo Get()
 */
public class ReidDemo extends HttpServlet {

    public static final  long serialVersionID = 1L;

    //继承默认构造方法
    public ReidDemo(){

        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String title = "使用 GET 方法读取表单数据";
        // 处理中文 服务初始页面
        if(null == request.getParameter("name"))
        {
            String docType = "<!DOCTYPE html> \n";
            out.println(docType +
                    "<html>\n" +
                    "<head><title>" + "菜鸟教程(runoob.com)" + "</title></head>\n" +
                    "<body bgcolor=\"#f0f0f0\">\n" +
                    "<h1 align=\"center\">" + "菜鸟教程(runoob.com)" + "</h1>\n" +
                    "<ul>\n" +
                    "  <form action=\"ReidDemo1\" method=\"GET\">\n" +
                    "网址名:<input type=\"text\" name=\"name\">\n" +
                    "<br />\n" +
                    "网址:<input type=\"text\" name=\"url\" />\n" +
                    "<input type=\"submit\" value=\"提交\" />\n" +
                    "</form>"
                    + "\n" +
                    "</ul>\n" +
                    "</body></html>");
        }
        else{
            String name = new String(request.getParameter("name").getBytes("ISO8859-1"), "UTF-8");
            String docType = "<!DOCTYPE html> \n";
            out.println(docType +
                    "<html>\n" +
                    "<head><title>" + title + "</title></head>\n" +
                    "<body bgcolor=\"#f0f0f0\">\n" +
                    "<h1 align=\"center\">" + title + "</h1>\n" +
                    "<ul>\n" +
                    "  <li><b>站点名</b>:"
                    + name + "\n" +
                    "  <li><b>网址</b>:"
                    + request.getParameter("url") + "\n" +
                    "</ul>\n" +
                    "</body></html>");
        }
    }

}

效果图:

 

使用Get方法提交:

<form action=\"ReidDemo1\" method=\"GET\">

action 必须和servlet name保持一致:

<servlet-name>ReidDemo1</servlet-name>

 

提交:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值