時間: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>之間。關鍵詞:示例,簡單