• Java中前台JSP请求Servlet实例(http+Servlet)


    1、前台jsp代码 himily.jsp,定义了用户名和密码两个输入框,使用post方式提交:/order-web/HimilyServlet其中order-web为站点名称,HimilyServlet为servlet访问路径

    <%@ 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=utf-8">
    <title>Welcome to here</title>
    </head>
    <body>
        <form action="/order-web/HimilyServlet" method="Post">
            用户名:<input type="text" value="请输入用户名" name="user" size="20px"><br>
            密码:<input type="password" value="请输入密码" name="pswd" size="20px"><br>
            
            <input type="submit" value="提交" size="10px">
        </form>
    </body>
    </html>
    himily.jsp


     

    2、servlet层代码:MyServlet.java

    新建MyServlet并且指定servlet的路径为/HimilyServlet

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
    
    
    
      <display-name>Archetype Created Web Application</display-name>
      <servlet>
          <servlet-name>OrderServlet</servlet-name>
          <display-name>OrderServlet</display-name>
          <description></description>
          <servlet-class>com.suning.logistics.order.web.OrderServlet</servlet-class>
      </servlet>
      <servlet>
          <servlet-name>QueryOrder</servlet-name>
          <display-name>QueryOrder</display-name>
          <description></description>
          <servlet-class>com.suning.logistics.order.web.QueryOrder</servlet-class>
      </servlet>
      <servlet>
          <servlet-name>MyServlet</servlet-name>
          <display-name>MyServlet</display-name>
          <description></description>
          <servlet-class>com.suning.logistics.order.web.MyServlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>OrderServlet</servlet-name>
          <url-pattern>/OrderServlet</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
          <servlet-name>QueryOrder</servlet-name>
          <url-pattern>/QueryOrder.do</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
          <servlet-name>MyServlet</servlet-name>
          <url-pattern>/HimilyServlet</url-pattern>
      </servlet-mapping>
    
    </web-app>
    web.xml

    MyServlet.java代码,这里是接受客户端的请求以及处理后的响应

     方法一:

    Map<String,String[]> ps = request.getParameterMap();

    获取客户端的所有请求参数的Key和Value值存入一个Map中,并且Value是数组类型,便于有相同key的时候组成二维数组

     1 package com.suning.logistics.order.web;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 import java.security.Principal;
     6 import java.util.Map;
     7 
     8 import javax.servlet.ServletException;
     9 import javax.servlet.http.HttpServlet;
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 
    13 /**
    14  * Servlet implementation class MyServlet
    15  */
    16 public class MyServlet extends HttpServlet {
    17     private static final long serialVersionUID = 1L;
    18        
    19     /**
    20      * @see HttpServlet#HttpServlet()
    21      */
    22     public MyServlet() {
    23         super();
    24         // TODO Auto-generated constructor stub
    25     }
    26 
    27     /**
    28      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    29      */
    30     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    31         try{
    32         //System.out.println("himily's servlet!");
    33         //response.getWriter().write("aaaa");
    34         request.setCharacterEncoding("utf-8");    
    35         response.setContentType("text/html;charset=utf-8");
    36         String user = request.getParameter("user");
    37         String pswd = request.getParameter("pswd");
    38         response.setStatus(200);
    39         PrintWriter pw=response.getWriter();
    40         pw.write("<html><body><p>登录成功!
    你输入的用户名为:"+user+"
    你输入的密码为:"+pswd+"</p></body></html>");
    41         pw.close();
    42         }catch(Exception e){
    43             response.getWriter().write(e.toString());
    44         }
    45             
    46         
    47     }
    48 
    49     /**
    50      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    51      */
    52     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    53         // TODO Auto-generated method stub
    54         //doGet(request,response);
    55         Map<String,String[]> ps = request.getParameterMap();
    56         request.setCharacterEncoding("utf-8");    
    57         response.setContentType("text/html;charset=utf-8");
    58     //    String user = request.getParameter("user");
    59     //    String pswd = request.getParameter("pswd");
    60         response.setStatus(200);
    61         PrintWriter pw=response.getWriter();
    62         pw.write("<html><body><p>登录成功!
    你输入的用户名为:"+ps.get("user")[0]+"
    你输入的密码为:"+ps.get("pswd")[0]+"</p></body></html>");
    63         pw.close();
    64     }
    65 
    66 }
    MyServlet.java

    方法二:

      //获取客户端POST参数的值
      String user = request.getParameter("user");
      String pswd = request.getParameter("pswd");

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            //doGet(request,response);
            //Map<String,String[]> ps = request.getParameterMap();
            request.setCharacterEncoding("utf-8");    
            response.setContentType("text/html;charset=utf-8");
            //获取客户端POST参数的值
            String user = request.getParameter("user");
            String pswd = request.getParameter("pswd");
            response.setStatus(200);
            PrintWriter pw=response.getWriter(); //输出响应信息到客户端
            pw.write("<html><body><p>登录成功!
    你输入的用户名为:"+user+"
    你输入的密码为:"+pswd+"</p></body></html>");
            pw.close();
        }
    servlet方法二

    3、访问地址:
    POST访问地址为:http://localhost:9008/order-web/himily.jsp

    Get方式访问地址:http://localhost:9008/order-web/HimilyServlet?user=aaa&pswd=bbbb

    直接访问Servlet的地址:http://localhost:9008/order-web/HimilyServlet

  • 相关阅读:
    mysql对表操作的各种语句
    Map遍历两种方式
    hibernate3
    Spring、mybaits整合
    mybaits注解
    mybaits 框架运用
    mybatis入门
    限制文本框字符数
    Unity3D Mathf函数
    Unity3d 粒子工具注释
  • 原文地址:https://www.cnblogs.com/trhimily/p/5924319.html
Copyright © 2020-2023  润新知