uniapp开发app框架在提升开发效率中的独特优势与应用探索
789
2022-11-06
实现一个简单的servlet实例
简介:servlet是JavaWeb三大组件之一,是运行在服务器中的小型Java程序,用于接收和响应客户端的请求。一个Servlet的生命周期由实例化、初始化、提供服务、销毁几个步骤组成。
MyServlet01.java
package com.sl.servlet; import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.javax.servlet.javax.servlet./** * MyServlet01 */@WebServlet("/MyServlet01")public class MyServlet01 extends HttpServlet { private static final long serialVersionUID = 1L; public MyServlet01() { System.out.println("****MyServlet01实例化****"); } @Override public void init() throws ServletException { System.out.println("****MyServlet01初始化****"); } @Override public void destroy() { System.out.println("****MyServlet01被销毁****"); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.service(req, resp); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); String account = request.getParameter("account"); String psd = request.getParameter("psd"); if(account.equals("admin") && psd.equals("123456")) { response.sendRedirect("success.jsp"); System.out.println("账号:"+account+"密码:"+psd+"登录成功"); return; }else { response.sendRedirect("index.jsp"); System.out.println("账号:"+account+"密码:"+psd+"登录失败"); return; } } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~