⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dispatcherservlet.java

📁 Tomcat与Java.Web开发技术详解源代码
💻 JAVA
字号:

package mypack;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class DispatcherServlet extends HttpServlet {

  private String target = "/hello.jsp";

  public void init(ServletConfig config)
    throws ServletException {
    super.init(config);

    Properties ps=new Properties();
    Properties ps_ch=new Properties();
    try{
      ServletContext context=config.getServletContext();
      InputStream in=context.getResourceAsStream("/WEB-INF/messageresource.properties");
      ps.load(in);
      InputStream in_ch=context.getResourceAsStream("/WEB-INF/messageresource_ch.properties");
      ps_ch.load(in_ch);

      in.close();
      in_ch.close();

      context.setAttribute("ps",ps);
      context.setAttribute("ps_ch",ps_ch);
    }catch(Exception e){
      e.printStackTrace();
    }

  }

  public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    // If it is a get request forward to doPost()
    doPost(request, response);
  }

  public void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    // Get the username from the request
    String username = request.getParameter("username");
    // Get the password from the request
    String password = request.getParameter("password");

    // Add the  user to the request
    request.setAttribute("USER", username);
    request.setAttribute("PASSWORD", password);

    // Forward the request to the target named
    ServletContext context = getServletContext();

    System.out.println("Redirecting to " + target);
    RequestDispatcher dispatcher =
      context.getRequestDispatcher(target);
    dispatcher.forward(request, response);
  }

  public void destroy() {
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -