servlet如何向html传递参数?

servlet如何向html传递参数?,第1张

html静态页面是不能接收参数的,你可以把html页面改为jsp页面,实现如下servlet类package one.twoimport java.io.IOException

import java.io.PrintWriterimport javax.servlet.RequestDispatcher

import javax.servlet.ServletException

import javax.servlet.http.HttpServlet

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponsepublic class Servlet extends HttpServlet {

public Servlet() {

html静态页面是不能接收参数的,你可以把html页面改为jsp页面,实现如下servlet类package one.twoimport java.io.IOException

import java.io.PrintWriterimport javax.servlet.RequestDispatcher

import javax.servlet.ServletException

import javax.servlet.http.HttpServlet

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponsepublic class Servlet extends HttpServlet {

public Servlet() {

super()

}/** 好网 : http://www.ablanxue.com

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy()// Just puts "destroy" string in log

// Put your code here

} public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost( request, response)

} public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {request.setAttribute("param"," http://www.ablanxue.com")

RequestDispatcher requestDispatcher=request.getRequestDispatcher("/ablanxue.jsp")

requestDispatcher.forward(request,response)

} /**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}}

jsp页面接收参数 ablanxue.jsp代码如下<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath()

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"

%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'ablanxue.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

--> </head>

<body>

<%

String PAR=(String)request.getAttribute("param")

%>

上面这个PAR就是得到的参数了输入出

<%=PAR%>

页面会输出 : http://www.ablanxue.com

<br>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6223808.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-18
下一篇 2023-03-18

发表评论

登录后才能评论

评论列表(0条)

保存