zoukankan      html  css  js  c++  java
  • Servlet简单的使用方法

    Servlet是什么??

    Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。

    使用 Servlet,可以收集来自网页表单的用户输入,呈现来自数据库或者其他源的记录,还可以动态创建网页。

    Java Servlet 通常情况下与使用 CGI(Common Gateway Interface,公共网关接口)实现的程序可以达到异曲同工的效果。但是相比于 CGI,Servlet 有以下几点优势:

    • 性能明显更好。
    • Servlet 在 Web 服务器的地址空间内执行。这样它就没有必要再创建一个单独的进程来处理每个客户端请求。
    • Servlet 是独立于平台的,因为它们是用 Java 编写的。
    • 服务器上的 Java 安全管理器执行了一系列限制,以保护服务器计算机上的资源。因此,Servlet 是可信的。
    • Java 类库的全部功能对 Servlet 来说都是可用的。它可以通过 sockets 和 RMI 机制与 applets、数据库或其他软件进行交互。

      编写Servlet,验证用户登录,如果用户名和密码都为"admin".则验证通过,跳转至欢迎页面,否则弹出提示框

      Servlet

    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class UserServlet extends HttpServlet  {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            super.doGet(req, resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            
            //设置网页编码格式
            req.setCharacterEncoding("UTF-8");
            resp.setContentType("text/html;charset=utf-8");
            
            String uName=req.getParameter("userName");
            String pwd=req.getParameter("pwd");
            if ("admin".equals(uName)&&"admin".equals(pwd)) {
                //获取和此次请求相关联的session.若果没有给客户端分配session,则创建一个新的session
                HttpSession session=req.getSession();
                session.setAttribute("login",uName);
                //重定向
                resp.sendRedirect("/Demo07/Welcome.jsp");
            }else {
                resp.setCharacterEncoding("UTF-8");
                //获取PrintWriter对象,用于向客户端发送文本
                PrintWriter outs=resp.getWriter();
                outs.println("<script type='text/javascript'>"+"alert('用户名或密码错误,请重新输入!');"
                +"location.href='/Demo07/index.jsp';</script>");
                outs.close();
            }
        }

      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="userServlet" method="post">
    用户名:<input type="text" name="userName" id="user"/><br>&nbsp;&nbsp;&nbsp;码:<input type="password" name="pwd" id="pwd" style="158px"/><br>
    <input type="submit" value="提交"/>
    </form>
    </body>
    </html>

    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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>Demo07</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
          </welcome-file-list>
    
       
        <servlet>
        <servlet-name>userServlet</servlet-name>
        <servlet-class>com.yjc.practice1.UserServlet</servlet-class>
        </servlet>
       
        
        <servlet-mapping>
        <servlet-name>userServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
        </servlet-mapping>
    
    </web-app>
  • 相关阅读:
    [LC] 71. Simplify Path
    [LC] 225. Implement Stack using Queues
    [Coding Made Simple / LeetCode 1235] Maximum Profit in Job Scheduling
    [Coding Made Simple] Cutting Rod for max profit
    [Coding Made Simple] Longest Common Substring
    [GeeksForGeeks] Convert an array to reduced form
    [GeeksForGeeks] Find if there is a pair with a given sum in a sorted and rotated array.
    [Coding Made Simple] Optimal Binary Search Tree
    [GeeksForGeeks] Write a program to delete a tree
    [GeeksForGeeks] Check if two trees are Isomorphic
  • 原文地址:https://www.cnblogs.com/yjc1605961523/p/11143359.html
Copyright © 2011-2022 走看看