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" /><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>
  • 相关阅读:
    yocto/bitbake 学习资源
    QEMU/KVM学习资源
    ubuntu 中创建和删除用户
    git 重命名本地和远程分支
    Ubuntu 上搭建 FTP 服务器
    gdb 常见用法
    git log 显示与特定文件相关的 commit 信息
    基于 qemu system mode 运行 arm 程序
    基于 qemu user mode 运行 aarch64 程序
    checking in(airport)
  • 原文地址:https://www.cnblogs.com/chuliuxiang/p/11149133.html
Copyright © 2011-2022 走看看