初始servlet之一

DemoServlet.java

package com.web;

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

public class DemoServlet  implements Servlet {
    public DemoServlet(){
        System.out.println("demoServlet");
    }
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("初始化1");
        //获得init-param的值,针对单个servlet
       String  myencoding = config.getInitParameter("myencoding");
        System.out.println("String myencoding="+myencoding);
        //获得context-param的值,针对所有的servlet
        String code = config.getServletContext().getInitParameter("code");
        System.out.println("code="+code);
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override //默调取servlet方法实现业务逻辑
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("DemoServlet->sessionid="+((HttpServletRequest)servletRequest).getSession().getId());
        System.out.println("执行业务逻辑的方法");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("销毁servlet的方法");
    }
}

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 <h1>登录</h1>
<a href="tologin">登录</a>
<form action="login" method="post">
    username:<input type="text" name="username"> <br>
    password:<input type="password" name="password"><br>
    爱好: <input type="checkbox" name="love" value="lanqiu"> 篮球  <input type="checkbox" name="love" value="paiqiu"> 排球 <input type="checkbox" name="love" value="yumaoqiu"> 羽毛球  <br>
    <input type="submit" value="登录">
</form>
</body>
</html>

LoginServlet .java

package com.web;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理get请求
       // req.getRequestDispatcher("loginError.html").forward(req,resp);
        //由后台给前端返回一段js代码
        resp.setContentType("text/html;charset=utf-8");
       PrintWriter writer =  resp.getWriter();
       writer.print("<script>alert('请根据表单进行登录');location.href='login.html'</script>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet->sessionid="+req.getSession().getId());
        System.out.println("session的非活动时间的默认值:"+req.getSession().getMaxInactiveInterval()); //默认时间单位是秒
        //处理post请求
        //req.setCharacterEncoding("utf-8"); // 一定要在接收参数的前面
        req.setCharacterEncoding(myencoding); //初始化myencoding
        //1、接收参数
        String username =  req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("username="+username+",password="+password);
        String[] loves = req.getParameterValues("love");
        for (String love:loves) {
            System.out.println("love="+love);
        }

        //2、跳转页面
        if("admin".equals(username)){
            //req.getRequestDispatcher("/success.html").forward(req,resp);
            resp.sendRedirect("success.html");   //重定向
        }else{
            req.getRequestDispatcher("/error.html").forward(req,resp);  //转发
        }
    }

    @Override
    public void destroy() {
        //销毁方法
    }


    String myencoding = null;
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("初始化1");
        myencoding = config.getInitParameter("myencoding");
        System.out.println("String myencoding="+myencoding);

        //获得context-param的值,针对所有的servlet
        String code = config.getServletContext().getInitParameter("code");
        System.out.println("code="+code);
    }
}

loginError.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>登录时必须使用post请求</h1>
</body>
</html>

success.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success.html,登录成功</h1>
</body>
</html>

error.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>error.html,登录失败</h1>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>aa</servlet-name>
        <servlet-class>com.lzy.web.DemoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>aa</servlet-name>
        <url-pattern>/tologin</url-pattern>
    </servlet-mapping>


    <context-param>
        <param-name>code</param-name>
        <param-value>GBK</param-value>
    </context-param>
    <servlet>
        <servlet-name>bb</servlet-name>
        <servlet-class>com.lzy.web.LoginServlet</servlet-class>
        <init-param>
            <param-name>myencoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>bb</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>


</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值