• 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>
  • 相关阅读:
    HTML基础之HTML标签-html header(meta,title) html body(p,br,h,form,div,span,input,lable)
    day5_非空即真非零即真
    day5_不能循环删除list-深拷贝、浅拷贝(import copy)
    day5_函数返回值
    day4_局部变量和全局变量
    day5_函数的参数
    python字符串与列表的相互转换
    python自动化返回:no tests were fount Empty test suite
    读取Excel数据封装
    测试工程题
  • 原文地址:https://www.cnblogs.com/boshen-hzb/p/6265046.html
Copyright © 2020-2023  润新知