js获取request中的参数需要使用脚本参数获取的格式:<%=xxx=%>来获取。
举例如下:
<%@ page language="java" import="java.util.*,com.servlet.bean" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
2、在servlet服务端的代码如下:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class servlet extends HttpServlet {
public servlet() {
super();
}
/**
* Destruction of the servlet.
*/
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 {
bean Bean = new bean();
Bean.setName("liubin");
Bean.setPwd("123");
bean Bean2 = new bean();
Bean2.setName("aaa");
Bean2.setPwd("456");
bean Bean3 = new bean();
Bean3.setName("bbb");
Bean3.setPwd("789");
ArrayList list = new ArrayList();
list.add(Bean);
list.add(Bean2);
list.add(Bean3);
request.setAttribute("list", list);
request.getRequestDispatcher("../index.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request ,response);
}
public void init() throws ServletException {
// Put your code here
}
}