Servlet:Servlet与JSP九大内置对象的对应关系以及通过Servlet获取表单数据

    我们说,Servlet是JSP的前身,那么对应于JSP九大内置对象在Servlet中是如何实现的呢?
1、out—–>resp.getWriter()
这两个类型在严格意义上是不匹配的,out对象是一个JSPWriter类型的对象,而resp.getWriter()获取的对象则是printWriter类型的对象
2、request—–>service()方法中的req参数
3、response—–>service()方法中的resp参数
4、session—–>req.getSession()函数
5、application—–>getServletContext()函数
6、exception—–>Throwable
7、page—–>this
8、pageContext—–>PageContext
9、Config—–getServletConfig()函数
接下来我们来看看如何通过Servlet来获取表单数据:
首先建立一个简单的表单数据提交页面:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=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>填写信息页</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>
  <%
  response.getContentType();
   %>
  <form action="Servlet/Test3" method="post">
  姓名:<input type="text" name="username">
  爱好:乒乓<input type="checkbox" name="hobbies" value="乒乓球"> 
 <input type="checkbox" name="accept" value="1">是否接受条款 
 <input type="submit" value="提交">
  </form>
  </body>
</html>

接下来我们从Servlet中获取表单数据:

package servlet;

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

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

import user.user;

public class test3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doPost(req,resp);
}
@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setHeader("content-type","text/html;charset=UTF-8");
    response.setCharacterEncoding("utf-8"); 
    request.setCharacterEncoding("utf-8");
    String username;
    String[] hobbies;
    boolean isAccept=false;
    username=request.getParameter("username");
    hobbies=request.getParameterValues("hobbies");
    if((request.getParameter("accept"))!=null){
        isAccept=true;
    }
    PrintWriter out =response.getWriter();
        out.println("这是你想看到的!");
         user u=new user();
            u.setUsername(username);
            u.setHobbies(hobbies);
            u.setAccept(isAccept);
            request.getSession().setAttribute("userInformation", u);        request.getRequestDispatcher("../information.jsp").forward(request, response);//由于现在所处位置为servlet这一子目录中,要跳转到外层目录中
    }
}

接下来我们在另一个页面中通过javaBean来获取到表单提交的信息

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="servlet.test3" %>
<%
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 'information.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>
 <jsp:useBean id="userInformation" class="user.user" scope="session"/>
 用户名:<jsp:getProperty property="username" name="userInformation"/>
爱好:
<%
String[] hobbies=userInformation.getHobbies();
for(int i=0;i<hobbies.length;i++){
out.println(hobbies[i]);
}
 %>
 <!--注意生成getter和setter时,isAccept字段的getter方法的名称为isAccept(),如果在这里使用<jsp:getProperty property="isAccept" name="userInformation" />是获取不到该字段的值的-->
 是否接受协议:<%=userInformation.isAccept() %>
  </body>
</html>

看一下表单提交的结果:

用户名:ni 爱好: 乒乓球 是否接受协议:true

还有一步xml文件的配置在前面的内容中已经介绍过,大家有需要的可以去参考一下~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值