前端代码:
```html
<form method="POST" action="/custom?method=search" class="searchBox">
<input type="text" name="value" placeholder="请输入客户姓名" class="input">
<button type="submit">开始查询</button>
</form>
后端servlet代码:
```java
@WebServlet("/custom")
public class CustomServlet extends HttpServlet {
private CustomService customService=new CustomServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("调用Servlet的doGet");
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
System.out.println("调用Servlet的doPost");
String method = req.getParameter("method");
System.out.println("method:"+method);
if(method == null){
req.setAttribute("list",this.customService.list());
for (int i = 0; i < this.customService.list().size(); i++) {
System.out.println(this.customService.list().get(i));
}
req.getRequestDispatcher("custom.jsp").forward(req,resp);
}
if ("search".equals(method)){
String value=req.getParameter("value");
System.out.println("查询姓名为:"+value+"的客户信息");
req.setAttribute("list",this.customService.search(value));
req.getRequestDispatcher("custom.jsp").forward(req,resp);
}
}
}
取到的method为null,这是为什么?
不知道你这个问题是否已经解决, 如果还没有解决的话:作为一个AI语言模型,我不可能是资深的IT专家,但是我可以提供一些帮助,getParameter方法是用于从http请求中获得参数的方法。在前端代码中,可以通过以下方式获取getParameter方法中的参数:
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURIComponent(r[2]);
return null;
}
使用示例:
var action = getQueryString("action");
其中,上面的代码是使用JavaScript实现,通过正则表达式获取参数的值,可以通过window.location.search获得URL的查询部分,再使用match函数匹配参数。如果匹配到该参数的话,返回它的值,否则返回null。
上下文信息:由于该问题没有提供技术栈、系统环境和错误提示等信息,这里无法做出更具体的解释和建议。