日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之12.Servlet基础(2)

發布時間:2023/12/20 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之12.Servlet基础(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
–提交表單的方法
? get
? post

–Servlet 生命周期
–使用Servlet 輸出HTML頁面
–獲得Servlet初始化參數
–頁面導航
? 請求重定向
–response.sendRedirect(“url”);

? 請求轉發
–request.getRequestDispatcher(“url”).forward(request,response);
? 請求包含
–request.getRequestDispatcher(“url”).include(request,response); ############Michael分割線################ 全局參數對全局生效,我們來測試一下 ServletBasic2.java package com.michael.servlet;????

import java.io.IOException;????
import java.io.PrintWriter;????
import java.util.Enumeration;????

import javax.servlet.ServletException;????
import javax.servlet.http.HttpServlet;????
import javax.servlet.http.HttpServletRequest;????
import javax.servlet.http.HttpServletResponse;????

public class ServletBasic2 extends HttpServlet {????

????????/**????
???????? * Constructor of the object.????
???????? */
????
????????public ServletBasic2() {????
????????????????super();????
????????}????

????????/**????
???????? * Destruction of the servlet. <br>????
???????? */
????
????????public void destroy() {????
????????????????super.destroy(); // Just puts "destroy" string in log????
????????????????// Put your code here????
????????}????

????????/**????
???????? * The doGet method of the servlet. <br>????
???????? *????
???????? * This method is called when a form has its tag value method equals to get.????
???????? *????
???????? * @param request the request send by the client to the server????
???????? * @param response the response send by the server to the client????
???????? * @throws ServletException if an error occurred????
???????? * @throws IOException if an error occurred????
???????? */
????
????????public void doGet(HttpServletRequest request, HttpServletResponse response)????
????????????????????????throws ServletException, IOException {????

????????????????response.setContentType("text/html");????
????????????????PrintWriter out = response.getWriter();????
????????????????out????
????????????????????????????????.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");????
????????????????out.println("<HTML>");????
????????????????out.println("????<HEAD><TITLE>A Servlet</TITLE></HEAD>");????
????????????????out.println("????<BODY>");????
????????????????out.print("????????This is ");????
????????????????out.print(this.getClass());????
????????????????out.println(", using the GET method");????
????????????????out.println("????</BODY>");????
????????????????out.println("</HTML>");????
????????????????out.flush();????
????????????????out.close();????
????????}????

????????/**????
???????? * The doPost method of the servlet. <br>????
???????? *????
???????? * This method is called when a form has its tag value method equals to post.????
???????? *????
???????? * @param request the request send by the client to the server????
???????? * @param response the response send by the server to the client????
???????? * @throws ServletException if an error occurred????
???????? * @throws IOException if an error occurred????
???????? */
????
????????public void doPost(HttpServletRequest request, HttpServletResponse response)????
????????????????????????throws ServletException, IOException {????

????????????????response.setContentType("text/html");????
????????????????PrintWriter out = response.getWriter();????
????????????????out????
????????????????????????????????.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");????
????????????????out.println("<HTML>");????
????????????????out.println("????<HEAD><TITLE>A Servlet</TITLE></HEAD>");????
????????????????out.println("????<BODY>");????
????????????????out.print("????????This is ");????
????????????????out.print(this.getClass());????
????????????????out.println(", using the POST method");????
????????????????out.println("????</BODY>");????
????????????????out.println("</HTML>");????
????????????????out.flush();????
????????????????out.close();????
????????}????

????????/**????
???????? * Initialization of the servlet. <br>????
???????? *????
???????? * @throws ServletException if an error occure????
???????? */
????
????????public void init() throws ServletException {????
????????????????Enumeration enu = this.getServletContext().getInitParameterNames();????
????????????????while(enu.hasMoreElements()){????
????????????????????????String name = (String) enu.nextElement();????
????????????????????????String value = this.getServletContext().getInitParameter(name);????
????????????????????????System.out.println(name+":"+value);????
????????????????}????
????????}????

}
測試一下
–局部參數
? web.xml 配置
? 獲得 web.xml <?xml version="1.0" encoding="UTF-8"?>????
<web-app version="2.4"????
????????xmlns="http://java.sun.com/xml/ns/j2ee"????
????????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"????
????????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee????
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">????
????????<context-param>????
????????????????<param-name>driver</param-name>????
????????????????<param-value>com.mysql.jdbc.Driver</param-value>????
????????</context-param>????
????????<context-param>????
????????????????<param-name>url</param-name>????
????????????????<param-value>jdbc:mysql://localhost:3306/jdbc_db</param-value>????
????????</context-param>????
????<servlet>????
????????<servlet-name>ServletBasic</servlet-name>????
????????<servlet-class>com.michael.servlet.ServletBasic</servlet-class>????
????????<init-param>????
????????????????<param-name>username</param-name>????
????????????????<param-value>michael</param-value>????
????????</init-param>????
????????<init-param>????
????????????????<param-name>password</param-name>????
????????????????<param-value>111222</param-value>????
????????</init-param>????
????</servlet>????
????<servlet>????
????????<servlet-name>ServletBasic2</servlet-name>????
????????<servlet-class>com.michael.servlet.ServletBasic2</servlet-class>????
????</servlet>????

????<servlet-mapping>????
????????<servlet-name>ServletBasic</servlet-name>????
????????<url-pattern>/servlet/ServletBasic</url-pattern>????
????</servlet-mapping>????
????<servlet-mapping>????
????????<servlet-name>ServletBasic2</servlet-name>????
????????<url-pattern>/servlet/ServletBasic2</url-pattern>????
????</servlet-mapping>????

</web-app>
ServletBasic.java package com.michael.servlet;????

import java.io.IOException;????
import java.io.PrintWriter;????
import java.util.Enumeration;????

import javax.servlet.ServletException;????
import javax.servlet.http.HttpServlet;????
import javax.servlet.http.HttpServletRequest;????
import javax.servlet.http.HttpServletResponse;????

public class ServletBasic extends HttpServlet {????

????????/**????
???????? * Constructor of the object.????
???????? */
????
????????public ServletBasic() {????
????????????????super();????
????????????????System.out.println("-----ServletBasic-----");????
????????}????

????????/**????
???????? * Destruction of the servlet. <br>????
???????? */
????
????????public void destroy() {????
????????????????super.destroy();????
????????????????System.out.println("-----destroy-----");????
????????}????

????????/**????
???????? * The doGet method of the servlet. <br>????
???????? *????
???????? * This method is called when a form has its tag value method equals to get.????
???????? *????
???????? * @param request the request send by the client to the server????
???????? * @param response the response send by the server to the client????
???????? * @throws ServletException if an error occurred????
???????? * @throws IOException if an error occurred????
???????? */
????
????????public void doGet(HttpServletRequest request, HttpServletResponse response)????
????????????????????????throws ServletException, IOException {????
????????????????System.out.println("-----doGet-----");????

????????????????response.setContentType("text/html");????
????????????????PrintWriter out = response.getWriter();????
????????????????out????
????????????????????????????????.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");????
????????????????out.println("<HTML>");????
????????????????out.println("????<HEAD><TITLE>A Servlet</TITLE></HEAD>");????
????????????????out.println("????<BODY>");????
????????????????out.print("????????This is ");????
????????????????out.print(this.getClass());????
????????????????out.println(", using the GET method");????
????????????????out.println("????</BODY>");????
????????????????out.println("</HTML>");????
????????????????out.flush();????
????????????????out.close();????
????????}????

????????/**????
???????? * The doPost method of the servlet. <br>????
???????? *????
???????? * This method is called when a form has its tag value method equals to post.????
???????? *????
???????? * @param request the request send by the client to the server????
???????? * @param response the response send by the server to the client????
???????? * @throws ServletException if an error occurred????
???????? * @throws IOException if an error occurred????
???????? */
????
????????public void doPost(HttpServletRequest request, HttpServletResponse response)????
????????????????????????throws ServletException, IOException {????
????????????????System.out.println("-----doPost-----");????
????????????????response.setContentType("text/html");????
????????????????PrintWriter out = response.getWriter();????
????????????????out????
????????????????????????????????.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");????
????????????????out.println("<HTML>");????
????????????????out.println("????<HEAD><TITLE>A Servlet</TITLE></HEAD>");????
????????????????out.println("????<BODY>");????
????????????????out.print("????????This is ");????
????????????????out.print(this.getClass());????
????????????????out.println(", using the POST method");????
????????????????out.println("????</BODY>");????
????????????????out.println("</HTML>");????
????????????????out.flush();????
????????????????out.close();????
????????}????

????????/**????
???????? * Initialization of the servlet. <br>????
???????? *????
???????? * @throws ServletException if an error occure????
???????? */
????
????????public void init() throws ServletException {????
????????????????/*????
????????????????//第一種方法????
????????????????String driver = this.getServletContext().getInitParameter("driver");????
????????????????String url = this.getServletContext().getInitParameter("url");????
????????????????System.out.println(driver);????
????????????????System.out.println(url);????
????????????????//第二種方法????
????????????????Enumeration enu = this.getServletContext().getInitParameterNames();????
????????????????while(enu.hasMoreElements()){????
????????????????????????String name = (String) enu.nextElement();????
????????????????????????String value = this.getServletContext().getInitParameter(name);????
????????????????????????System.out.println(name+":"+value);????
????????????????}????
????????????????*/
????
????????????????String username = this.getInitParameter("username");????
????????????????String password = this.getInitParameter("password");????
????????????????System.out.println(username);????
????????????????System.out.println(password);????
????????????????System.out.println("-----init-----");????
????????}????

????????@Override????
????????protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {????
????????????????// TODO Auto-generated method stub????
????????????????System.out.println("-----service-----");????
????????}????
}
ServletBasic2.java package com.michael.servlet;????

import java.io.IOException;????
import java.io.PrintWriter;????
import java.util.Enumeration;????

import javax.servlet.ServletException;????
import javax.servlet.http.HttpServlet;????
import javax.servlet.http.HttpServletRequest;????
import javax.servlet.http.HttpServletResponse;????

public class ServletBasic2 extends HttpServlet {????

????????/**????
???????? * Constructor of the object.????
???????? */
????
????????public ServletBasic2() {????
????????????????super();????
????????}????

????????/**????
???????? * Destruction of the servlet. <br>????
???????? */
????
????????public void destroy() {????
????????????????super.destroy(); // Just puts "destroy" string in log????
????????????????// Put your code here????
????????}????

????????/**????
???????? * The doGet method of the servlet. <br>????
???????? *????
???????? * This method is called when a form has its tag value method equals to get.????
???????? *????
???????? * @param request the request send by the client to the server????
???????? * @param response the response send by the server to the client????
???????? * @throws ServletException if an error occurred????
???????? * @throws IOException if an error occurred????
???????? */
????
????????public void doGet(HttpServletRequest request, HttpServletResponse response)????
????????????????????????throws ServletException, IOException {????

????????????????response.setContentType("text/html");????
????????????????PrintWriter out = response.getWriter();????
????????????????out????
????????????????????????????????.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");????
????????????????out.println("<HTML>");????
????????????????out.println("????<HEAD><TITLE>A Servlet</TITLE></HEAD>");????
????????????????out.println("????<BODY>");????
????????????????out.print("????????This is ");????
????????????????out.print(this.getClass());????
????????????????out.println(", using the GET method");????
????????????????out.println("????</BODY>");????
????????????????out.println("</HTML>");????
????????????????out.flush();????
????????????????out.close();????
????????}????

????????/**????
???????? * The doPost method of the servlet. <br>????
???????? *????
???????? * This method is called when a form has its tag value method equals to post.????
???????? *????
???????? * @param request the request send by the client to the server????
???????? * @param response the response send by the server to the client????
???????? * @throws ServletException if an error occurred????
???????? * @throws IOException if an error occurred????
???????? */
????
????????public void doPost(HttpServletRequest request, HttpServletResponse response)????
????????????????????????throws ServletException, IOException {????

????????????????response.setContentType("text/html");????
????????????????PrintWriter out = response.getWriter();????
????????????????out????
????????????????????????????????.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");????
????????????????out.println("<HTML>");????
????????????????out.println("????<HEAD><TITLE>A Servlet</TITLE></HEAD>");????
????????????????out.println("????<BODY>");????
????????????????out.print("????????This is ");????
????????????????out.print(this.getClass());????
????????????????out.println(", using the POST method");????
????????????????out.println("????</BODY>");????
????????????????out.println("</HTML>");????
????????????????out.flush();????
????????????????out.close();????
????????}????

????????/**????
???????? * Initialization of the servlet. <br>????
???????? *????
???????? * @throws ServletException if an error occure????
???????? */
????
????????public void init() throws ServletException {????
????????????????/*????
????????????????Enumeration enu = this.getServletContext().getInitParameterNames();????
????????????????while(enu.hasMoreElements()){????
????????????????????????String name = (String) enu.nextElement();????
????????????????????????String value = this.getServletContext().getInitParameter(name);????
????????????????????????System.out.println(name+":"+value);????
????????????????}????
????????????????*/
????
????????????????String username = this.getInitParameter("username");????
????????????????String password = this.getInitParameter("password");????
????????????????System.out.println(username);????
????????????????System.out.println(password);????
????????}????

}
測試 現在調用第二個 ? 頁面導航
– 請求重定向
? response.sendRedirect(“url”);
– 請求轉發
? request.getRequestDispatcher(“url”).forward(request,response);
– 請求包含

? request.getRequestDispatcher(“url”).forward(request,response); ServletBasic.java package com.michael.servlet;????

import java.io.IOException;????
import java.io.PrintWriter;????
import java.util.Enumeration;????

import javax.servlet.ServletException;????
import javax.servlet.http.HttpServlet;????
import javax.servlet.http.HttpServletRequest;????
import javax.servlet.http.HttpServletResponse;????

public class ServletBasic extends HttpServlet {????

????????/**????
???????? * Constructor of the object.????
???????? */
????
????????public ServletBasic() {????
????????????????super();????
????????????????System.out.println("-----ServletBasic-----");????
????????}????

????????/**????
???????? * Destruction of the servlet. <br>????
???????? */
????
????????public void destroy() {????
????????????????super.destroy();????
????????????????System.out.println("-----destroy-----");????
????????}????

????????/**????
???????? * The doGet method of the servlet. <br>????
???????? *????
???????? * This method is called when a form has its tag value method equals to get.????
???????? *????
???????? * @param request the request send by the client to the server????
???????? * @param response the response send by the server to the client????
???????? * @throws ServletException if an error occurred????
???????? * @throws IOException if an error occurred????
???????? */
????
????????public void doGet(HttpServletRequest request, HttpServletResponse response)????
????????????????????????throws ServletException, IOException {????
????????????????String username = request.getParameter("username");????
????????????????String password = request.getParameter("password");????
????????????????System.out.println("username="+username);????
????????????????if(username!=null&&username.equals("redking")){????
????????????????????????request.getRequestDispatcher("/successfull.html").forward(request,response);????
????????????????}else{????
????????????????????????request.getRequestDispatcher("/failure.html").forward(request,response);????
????????????????}????
????????????????/*????
????????????????System.out.println("-----doGet-----");????

????????????????response.setContentType("text/html");????
????????????????PrintWriter out = response.getWriter();????
????????????????out????
????????????????????????????????.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");????
????????????????out.println("<HTML>");????
????????????????out.println("????<HEAD><TITLE>A Servlet</TITLE></HEAD>");????
????????????????out.println("????<BODY>");????
????????????????out.print("????????This is ");????
????????????????out.print(this.getClass());????
????????????????out.println(", using the GET method");????
????????????????out.println("????</BODY>");????
????????????????out.println("</HTML>");????
????????????????out.flush();????
????????????????out.close();????
????????*/
????????
????????}

????????/**????
???????? * The doPost method of the servlet. <br>????
???????? *????
???????? * This method is called when a form has its tag value method equals to post.????
???????? *????
???????? * @param request the request send by the client to the server????
???????? * @param response the response send by the server to the client????
???????? * @throws ServletException if an error occurred????
???????? * @throws IOException if an error occurred????
???????? */
????
????????public void doPost(HttpServletRequest request, HttpServletResponse response)????
????????????????????????throws ServletException, IOException {????
????????????????System.out.println("-----doPost-----");????
????????????????response.setContentType("text/html");????
????????????????PrintWriter out = response.getWriter();????
????????????????out????
????????????????????????????????.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");????
????????????????out.println("<HTML>");????
????????????????out.println("????<HEAD><TITLE>A Servlet</TITLE></HEAD>");????
????????????????out.println("????<BODY>");????
????????????????out.print("????????This is ");????
????????????????out.print(this.getClass());????
????????????????out.println(", using the POST method");????
????????????????out.println("????</BODY>");????
????????????????out.println("</HTML>");????
????????????????out.flush();????
????????????????out.close();????
????????}????

????????/**????
???????? * Initialization of the servlet. <br>????
???????? *????
???????? * @throws ServletException if an error occure????
???????? */
????
????????public void init() throws ServletException {????
????????????????/*????
????????????????//第一種方法????
????????????????String driver = this.getServletContext().getInitParameter("driver");????
????????????????String url = this.getServletContext().getInitParameter("url");????
????????????????System.out.println(driver);????
????????????????System.out.println(url);????
????????????????//第二種方法????
????????????????Enumeration enu = this.getServletContext().getInitParameterNames();????
????????????????while(enu.hasMoreElements()){????
????????????????????????String name = (String) enu.nextElement();????
????????????????????????String value = this.getServletContext().getInitParameter(name);????
????????????????????????System.out.println(name+":"+value);????
????????????????}????
????????????????*/
????
????????????????String username = this.getInitParameter("username");????
????????????????String password = this.getInitParameter("password");????
????????????????System.out.println(username);????
????????????????System.out.println(password);????
????????????????System.out.println("-----init-----");????
????????}????
????????/*????
????????@Override????
????????protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {????
????????????????// TODO Auto-generated method stub????
????????????????System.out.println("-----service-----");????
????????}????
????????*/
????
}
successfull.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">????
<html>????
????<head>????
????????<title>successfull.html</title>????
????????<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">????
????????<meta http-equiv="description" content="this is my page">????
????????<meta http-equiv="content-type" content="text/html; charset=UTF-8">????
????????<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->????

????</head>????
????<body>????
????????<h1>successfull!!!</h1>????
????</body>????
</html>
failure.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">????
<html>????
????<head>????
????????<title>failure.html</title>????
????????<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">????
????????<meta http-equiv="description" content="this is my page">????
????????<meta http-equiv="content-type" content="text/html; charset=UTF-8">????
????????<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->????

????</head>????
????<body>????
????????<h1>failure!!!</h1>????
????</body>????
</html>
輸入用戶名為redking進行測試 顯示successfull!!! 輸入其他用戶名測試 顯示failure!!! 看下顯示信息 ############Michael分割線################ 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之12.Servlet基础(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。