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

📄 actionservlet.java

📁 关于servlet的很全的例子,对学习servlet很有帮助的,大家
💻 JAVA
字号:
package com.allanlxf.sms.web;

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

public class ActionServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws IOException, ServletException
    {
        String path = request.getServletPath();
        path = path.substring(0, path.indexOf("."));

        if(path.equals("/login"))
        {
            String userName = request.getParameter("userName");
            String password = request.getParameter("password");
            if(getContextParameter("userName").equals(userName)
                && getContextParameter("password").equals(password))
            {
                HttpSession session = request.getSession();
                session.setAttribute("userName", userName);
                request.setAttribute("data", "request");
                response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/protected/user/center"));
                
                return;
            }else
            {
                response.sendRedirect(request.getContextPath() + "/login");
                return;
            }          
        }else if(path.equals("/logout"))
        {
            HttpSession session = request.getSession(false);
            session.invalidate();
            response.sendRedirect(request.getContextPath() + "/login");
            return;
        }else if(path.equals("/viewinfo"))
        {
            forward("/protected/user/info", request, response);
        }else if(path.equals("/modifyinfo"))
        {
            request.setAttribute("data", "request");
            forward("/protected/user/modify", request, response);
        }else
        {
            throw new ServletException("unknown path:" + path);
        }
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws IOException, ServletException
    {
        doGet(request, response);
    }
    
    private String getContextParameter(String name)
    {
        return getServletContext().getInitParameter(name);
    }
    
    private void forward(String path, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(path);
        dispatcher.forward(request, response);
    }
}

⌨️ 快捷键说明

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