• jsp调用java servlet


    1、依赖jar

    servlet-api.jar

    2、工程结构

    3、java servlet实现类

    package testServlet;
    
    import java.io.IOException;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ThreeParams extends HttpServlet
    {
      public void init(ServletConfig config)
        throws ServletException
      {
        System.out.println("init");
        super.init(config);
      }
    
      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
      {
        PrintWriter pw = resp.getWriter();
        System.out.println("Param1: " + req.getParameter("param1"));
        System.out.println("Param2: " + req.getParameter("param2"));
        System.out.println("Param3: " + req.getParameter("param3"));
        pw.println(req.getParameter("param1"));
        pw.println("<br>");
        pw.println(req.getParameter("param2"));
        pw.println("<br>");
        pw.println(req.getParameter("param3"));
        pw.println("<br>");
      }
    
      @Override
      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
      {
        System.out.println("post");
        super.doPost(req, resp);
      }
    }

    4、在配置文件中配置servlet

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    
    <web-app version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        
        <servlet>
            <servlet-name>ThreeParams</servlet-name>
            <servlet-class>testServlet.ThreeParams</servlet-class>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>ThreeParams</servlet-name>
            <url-pattern>/testServlet/ThreeParams</url-pattern>
        </servlet-mapping>
        
    </web-app>

    5、在jsp中调用servlet

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="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" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form id="form1" name="form1" method="get"  action="testServlet/ThreeParams" >
      <table width="343" border="1">
        <tr>
          <td width="92">param1</td>
          <td width="94"><input name="param1" type="text" id="param1" /></td>
        </tr>
        <tr>
          <td>param2</td>
          <td><label><input name="param2" type="text" id="param2" /></label></td>
        </tr>
        <tr>
          <td>param3</td>
          <td><label><input name="param3" type="text" id="param3" /></label></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" value="提交" /></td>
        </tr>
      </table>
    </form>
    </body>
    </html>
  • 相关阅读:
    [转]只有程序员才能看懂的幽默
    论安防集成管理平台规范标准建设的重要性和必要性
    视频集成技术演进与创新实践
    (转)jQuery中Ajax事件beforesend及各参数含义
    (转)如何打一手好Log
    marquee标签
    如何学习Git,如何使用Git
    学习vue
    python安装教程
    Jmeter压力测试(入门篇)
  • 原文地址:https://www.cnblogs.com/boshen-hzb/p/6265046.html
Copyright © 2020-2023  润新知