• 补充-servlet基础


    一、获取客户端信息

    1.建立一个动态web (Dynamic Web project)ServletTest的项目

    2 在工程.ServletTest中创建一个BookInformationServlet.java的Servlet。

    package com;

    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;


    /**
    * Servlet implementation class bookinformation
    */
    public class BookInformationServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public BookInformationServlet() {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out=response.getWriter();
    request.setCharacterEncoding("UTF-8");
    String price=request.getParameter("price");
    out.println("<html>");
    out.println("价格:"+price+"<br>");
    out.println("</html>");
    }

    }
    3.建立一个JSP显示页面 bookInformation.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <form name="form1" method="post" action="BookInformationServlet">
    <table>
    <tr>
    <td> 价格:<input name="price" type="text" id="name">
    </td>
    </tr>
    </table>
    </form>

    </body>
    </html>
    4.部署用于描述文件web.xml
     
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>ServletTest</display-name>
    <servlet>
    <servlet-name>SampleServlet</servlet-name>
    <servlet-class>com.SampleServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>SampleServlet</servlet-name>
    <url-pattern>/SampleServlet</url-pattern>
    </servlet-mapping>
    <servlet>
    <description></description>
    <display-name>BookInformationServlet</display-name>
    <servlet-name>BookInformationServlet</servlet-name>
    <servlet-class>com.BookInformationServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>BookInformationServlet</servlet-name>
    <url-pattern>/BookInformationServlet</url-pattern>
    </servlet-mapping>
      </web-app>
    5.结果如图

    J1671[S@PD$@PW@@YP{6K@N

    6RF7ASK3`2QBKD0MY38HCAR

    %%MGZB1OBJZKNNI(2X{P{5F

     
    二、Servlet之间通信
          出现在同一个web服务器上的servlet可皮此通信,也可共享资源。可用RequestDispatcher接口来实现Servlet之间的通信。即当前页面所要转
           发的目标页或servlet对象可用request获取用户提交的数据

    实现转发步骤:
    1、得到RequestDispatcher引用:
    public abstract RequestDispatcher getRequestDispatcher(String urlpath)
    其中urlpath为要转发Jsp页面或Servlet地址。
    RequestDispatcher dispatcher=request. getRequestDispatcher(a.jsp);

    RequestDispatcher dispatcher;
    dispatcher =getSeveltContext().

    getRequestDispatcher(/SecondServlet);

    2、转发:

    public abstract void forward(ServletRequest request,SevletResponse response)
                     throws ServletException,IOException
    该方法用来把一个Jsp或Servlet请求提交到另一个。当其输出完全由第二个Servlet或被调用Servlet生成时,必须用此方法。若第一个Servlet已访问了PrintWriter对象,则由此方法引发异常
    dispatcher.forward(request, response);

    例6-7 Servlet之间通信
    <%@ page contentType="text/html;charset=GB2312" %>
    <HTML>
    <BODY ><Font size=4>
    <form action="Verify" method="post">
    输入姓名:<Input Type=text name=name>
    <BR>输入分数:<Input Type=text name=score>
    <BR><Input Type=submit value="提交">
    </form>
    </Font>
    </BODY>
    </HTML>
    Verity.java
    package com;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Verify extends HttpServlet
    { public void init(ServletConfig config) throws ServletException
    {super.init(config);
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException
    { String name=request.getParameter("name"); //获取客户提交的信息
    String score=request.getParameter("score");
    if(name.length()==0||name==null)
    { response.sendRedirect("sixth_example5.jsp"); //重定向
    }
    else if(score.length()==0)
    { response.sendRedirect("sixth_example5.jsp"); //重定向
    }
    else if(score.length()>0)
    { try { int numberScore=Integer.parseInt(score);
    if(numberScore<0||numberScore>100)
    { response.sendRedirect("sixth_example5.jsp");
    }
    else
    { RequestDispatcher dispatcher=
    request.getRequestDispatcher("ShowMessage");
    dispatcher.forward(request, response); // 转发
    }
    }
    catch(NumberFormatException e)
    { response.sendRedirect("input.jsp");
    }
    }
    }
    public void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException
    { doPost(request,response);
    }
    }

    ShowMessage.java

    package com;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class ShowMessage extends HttpServlet
    { public void init(ServletConfig config) throws ServletException
    {super.init(config);
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException
    { response.setContentType("text/html;charset=utf-8");
    PrintWriter out=response.getWriter();
    String name=request.getParameter("name"); //获取客户提交的信息
    String score=request.getParameter("score"); //获取客户提交的信息
    try{ byte bb[]=name.getBytes("utf-8");
    name=new String(bb,"utf-8");
    }
    catch(Exception exp){}
    out.print("<Font color=blue size=4>你的姓名是:");
    out.print(name);
    out.print("<BR><Font color=pink size=4>你的成绩是:");
    out.print(score);
    }
    public void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException
    { doPost(request,response);
    }
    }

    9T1V1%]18KLT{AIC9@5SUPR

    D~J{1H{}}]U%3U4VGCYOQGP
  • 相关阅读:
    ActiveMQ中JMS的可靠性机制
    ActiveMQ中Broker的应用与启动方式
    ActiveMQ支持的传输协议
    ActiveMQ常见消息类型
    Oracle体系结构及备份(十六)——bg-ckpt
    PHP自学之路---雇员管理系统(1)
    UVa11187
    给Android组件添加事件一个很好用的方法
    【项目那些事儿】项目哪些事儿?
    struts2对拦截器使用带实例
  • 原文地址:https://www.cnblogs.com/elite-2012/p/3208403.html
Copyright © 2020-2023  润新知