zoukankan      html  css  js  c++  java
  • Get access to Servlet

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class LoginServlet extends HttpServlet
     {protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException
      { resp.setContentType("text/html;charset=gbk");
      PrintWriter out=resp.getWriter();
      try{
       String myUserName=this.getInitParameter("name");
       String myPassWord=this.getInitParameter("password");
                 HttpSession session=req.getSession();
       String scode=(String)session.getAttribute("code");
       String userName=req.getParameter("name");
       String passWord=req.getParameter("password");
       String code=req.getParameter("code");
       out.println("<html><body>");
       out.println("<br/>");
       if(!code.toUpperCase().equals(scode)){out.println("Wrong Validate!");}
       else if (userName.equals(myUserName)&&passWord.equals(myPassWord))
       {out.println("Login Sucessed! ");}
       else{out.println("Login Failed!");}
      }
       finally{out.close();}
      }
      protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
       doGet(req,resp);}
      
       

     }

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.OutputStream;
    import javax.imageio.ImageIO;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class CheckCodeServlet extends HttpServlet {
     public void doGet(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
      resp.setContentType("image/jpeg");
      OutputStream out = resp.getOutputStream();
      try {
       resp.setHeader("programa", "no-cache");
       resp.setHeader("Cache-Control", "no-cache");
       resp.setDateHeader("Expires", 0);
       BufferedImage image = new BufferedImage(50, 18,
         BufferedImage.TYPE_INT_RGB);
       Graphics g = image.getGraphics();
       g.setColor(Color.LIGHT_GRAY);
       g.fillRect(0, 0, 50, 18);
       g.setColor(Color.LIGHT_GRAY);
       g.drawRect(0, 0, 50, 18);
       String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       String code = "";
       for (int i = 0; i < 4; i++) {
        int k = (int) (Math.random() * 36);
        char c = str.charAt(k);
        code += c;
       }
       HttpSession session = req.getSession();
       session.setAttribute("code", code);
       g.setColor(Color.BLACK);
       Font font = new Font("DIALOG", Font.ITALIC, 15);
       g.setFont(font);
       g.drawString(code, 3, 15);
       ImageIO.write(image, "JPEG", out);
       out.flush();
       out.close();
      } finally {
       out.close();
      }
     }

     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      doGet(req,resp);}

    }

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <<html>
      <head>
        <title>Login</title>
     </head>
        <BODY bgColor=#ffffff leftMargin=0 text=#000000 topMargin=30><center>
        <form action="LoginServlet" method="get">
          User  <input maxlength=10 name="name" size=8><br>
        Password <input type=password name="password" size=8><br>
        Validate <input name=code size=8><br>
        <img src="checkCodeServlet" onclick="self.location.reload();"/><br>
    <input name=Submit type=submit value=Commit>
    <input name=Submit2 type=reset value=Reset>
      </form>
      </body>
    </html>

    <web-app>
         <servlet>
               <servlet-name>CheckCodeServlet</servlet-name>
               <servlet-class>Com.CheckCodeServlet</servlet-class>
        </servlet>

        <servlet>
               <servlet-name>LoingServlet</servlet-name>
               <servlet-class>Com.LoingServlet</servlet-class>

               <init-param>
                        <param-name>name</param-name>
                        <param-value>abc</param-value>
                </init-param>          
               <init-param>
                        <param-name>password</param-name>
                        <param-value>123</param-value>
                </init-param>          

        </servlet>

        <servlet-mapping>
               <servlet-name>CheckCodeServlet</servlet-name>
               <url-pattern>/CheckCodeServlet</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
               <servlet-name>LoingServlet</servlet-name>
               <url-pattern>/LoingServlet</url-pattern>
        </servlet-mapping>
    </web-app>


     

  • 相关阅读:
    Java学习笔记-Java中的常用类
    Java学习笔记-Java中的常用类
    Java学习笔记-泛型
    GeoIP简介与资源,定位经纬度,获取用户IP
    【插代码】手机号码所在地查询,引用代码
    最后一个对象属性后边不要加豆号的bug,血淋淋的教训啊,模块化开发IE7下的严重错误,养成好习惯
    CSS自定义滚动条样式
    css实现input文本框的双边框美化
    在函数内部定义的变量加与不加var的区别,匿名函数和有名函数内声明变量的区别
    js定时器关闭,js定时器停止,一次关闭所有正在运行的定时器,自定义函数clearIntervals()一次关闭所有正在运行的定时器
  • 原文地址:https://www.cnblogs.com/askDing/p/5328315.html
Copyright © 2011-2022 走看看