• JSP和Server的相互转化


    【设计想法】

       

    【前置条件】

        1. eclipse的web环境已经搭建OK

        2. eclipse已经存在web项目“firstweb”

    【操作步骤】

        1. “firstweb”新建3个jsp文件,hello.jsp, success.jsp, fail.jsp文件

          (1) hello.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=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="<%=request.getContextPath()%>/test" method="post">
            <table>
                <tr>
                    <td><label>用户名:</label></td>
                    <td><input name="username" type="text"></td>
                </tr>
                <tr>
                    <td><label>密码:</label></td>
                    <td><input name="password" type="text"></td>
                </tr>
                <tr>
                    <td><input name="sumbit" type="submit" value="Submit"></td>
                    <td><input name="cancel" type="reset" value="Cancel"></td>
    
                </tr>
            </table>
        </form>
    </body>
    </html>

          (2)success.jsp代码

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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>
    Sucess!
    </body>
    </html>

          (3)fail.jsp代码

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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>
     fail!
    </body>
    </html>

        2. “firstweb”新建1个server文件,test.java
          (1)test.java文件

    package com;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.Writer;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class test
     */
    public class test extends HttpServlet {
        private static final long serialVersionUID = 1L;
        PrintWriter pw = null;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public test() {
            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
            doPost(request, response);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            String username = (String) request.getParameter("username");
            String password = (String)request.getParameter("password");
            
            RequestDispatcher rd1 = request.getRequestDispatcher("success.jsp");
            RequestDispatcher rd2 = request.getRequestDispatcher("fail.jsp");
            if(username.equalsIgnoreCase("test") && password.equalsIgnoreCase("test")) {
                rd1.forward(request, response);
            } else {
                rd2.forward(request, response);
            }
    
        }
    
    }

        3. 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>firstweb</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <description></description>
        <display-name>test</display-name>
        <servlet-name>test</servlet-name>
        <servlet-class>com.test</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/test</url-pattern>
      </servlet-mapping>
    </web-app>

        4. 启动tomcat服务器   

        5. 运行返回正确的结果如下:
         

         

        6. 运行返回正确的结果如下:

         

        

  • 相关阅读:
    SpringCloud : yml文件配置 获取系统环境变量的值
    SpringCloud : 多个 @FeignClient 注解 value 设置为同一个应用的解决方案
    SpringCloud : Feign 不捆绑远程应用名称, 实现服务应用名称传入,调用通用自动化
    SpringCloud : Feign 使用 FastJson 解析数据
    Mybatis中在log日志或控制台打印执行的sql
    iOS 基于 itemServices 进行本地安装 ipa 应用安装包
    MySQL5.5 数据热备份
    如何在 Fiddler Script 中 自定义 修改 Request 、 Response
    Dubbo 分布式 日志 追踪
    shell脚本并发执行
  • 原文地址:https://www.cnblogs.com/zhuhaiying/p/6101881.html
Copyright © 2020-2023  润新知