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.IOExceptionimport 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>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)