java調用瀏覽器打開網頁,Servlet — 如何讓服務器控制瀏覽器10秒后跳轉到另一個頁面

 2023-12-25 阅读 29 评论 0

摘要:效果圖: index.html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="login" method="post"

效果圖:

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login"  method="post"><p>登錄名:<input name="loginName"  id="loginName"></p><p>密碼:<input name="passWord"  id="passWord"></p><!-- input的類型為submit  ,意味著點擊這個按鈕,會提交表單中的內容到action的屬性值submitTable的處理方法中去 --><input  type="submit"  value="登錄">
</form><br><br> 
</body>
</html>

java調用瀏覽器打開網頁、LoginServlet.java:

import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class LoginServlet extends  HttpServlet{String  loginName = "";@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//只有post請求才能用這種方式解決前端傳遞過來的中文數據亂碼的問題request.setCharacterEncoding("utf-8");//要獲取哪一個標簽的值,就根據標簽的name屬性值獲取<input name="loginName"loginName = request.getParameter("loginName");System.out.println("loginName:"+loginName);//指定響應到瀏覽器時,這些文字的編碼類型response.setCharacterEncoding("utf-8");//指定響應出去的代碼是什么類型的response.setContentType("text/html");try {//睡眠10秒Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}//響應輸出流,可將信息響應到頁面上PrintWriter  printWriter   =response.getWriter();printWriter.print("歡迎"+loginName+"登錄");}}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/194795.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息