时间:2023-05-30 20:06:02 | 来源:网站运营
时间:2023-05-30 20:06:02 来源:网站运营
最简单的JSP和Servlet示例:之前在一起认识Java web简单地提到过JSP和Serlet,今天我们就来跑跑JSP和Sevle,<%@ 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>登录界面</title></head><body> <form action="login "> 名字:<input type="text" name="name"></br> 密码:<input type="password" name="pwd"></br> <input type="submit" value="登录"> </form></body></html>
效果如图:public class Log extends HttpServlet{ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String name=request.getParameter("name"); String password=request.getParameter("pwd"); if(name.equals("刘耀")&&password.equals("61")){ System.out.println("登陆成功"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
代码分析:<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Survey</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>log</servlet-name> <servlet-class>Servlet.logservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>log</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping></web-app>
需要我们配置的只有两个部分<servlet>和</servlet>之间,和<servlet-mapping>和</servlet-mapping>之间。关键词:示例,简单