我正在尝试从我的jsp更改网页上的文本区域中的文本,然后我可以获取已更改的文本,然后使用API将其推送到网站。但每次我尝试在我的doPost方法中使用.getParameter(“textArea”)调用来从文本区域获取更改的文本时,当我尝试在控制台中将其打印出来时,它只会打印出null。我有一个GET方法,从网站获取我想要的信息,然后将信息的值放在文本区域。我也测试了我的帖子方法,它会发布到网站上。
这是我的Controller.java类,它有我的doGet和doPost方法:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import com.gurock.testrail.APIClient;
import com.gurock.testrail.APIException;
import java.util.Map;
import java.util.HashMap;
import org.json.simple.JSONObject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
public static String local; //String used to set attribute
//constructor
public Controller() {
super();
}
// *** doGet method ***
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String input = "";
try (PrintWriter out = response.getWriter()) {
if(request.getParameter("submitGet") != null) {
input = request.getParameter("input");
APIClient client = new APIClient("...");
client.setUser("..."); //set user name for login
client.setPassword("..."); //set password for login
try {
JSONObject c = (JSONObject) client.sendGet("get_case/" + input); //send a get request to the web page and get case ID
local = c.get("custom_test_script").toString(); //get the custom test script from web page
request.setAttribute("Local", local); //set the attribute of the test script with String local
}//try
catch (APIException e) { //Pulls from APIException java class
e.printStackTrace();
}//catch
}//if
//next two lines allows information that was output to stay on the same web page and not redirect too another page and allow more input
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/testCases.jsp");
requestDispatcher.forward(request, response);
}//try
}//doGet
// *** doPost Method ***
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) { //get input from web page
if(request.getParameter("submitPost") != null) {
String test_id=request.getParameter("test_id");
String textarea = request.getParameter("textArea");
APIClient client = new APIClient("...");
client.setUser("..."); //set user name for login
client.setPassword("..."); // set password for login
try {
Map data = new HashMap();
System.out.println(textarea);
data.put("custom_test_script", textarea);
client.sendPost("update_case/" + test_id, data);
}//try
catch (APIException e) {
e.printStackTrace();
}//catch
}//if
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/testCases.jsp");
requestDispatcher.forward(request, response);
}//try
}//doPost
}//Controller
这是我的.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="testCases.css">
<title>Test Cases</title>
</head>
<body>
<center><h1>Test Cases</h1></center>
<form action="Controller" method="get" id="getForm">
<label>Input Test Case ID #: </label>
<input type="text" name="input" value="">
<input type="submit" value="Submit" name="submitGet" class="btn btn-
primary"> <br><br><br><br>
<label>Test Case Test Script: </label>
<textarea name="textArea" id="output">
<%=request.getAttribute("Local")%></textarea>
</form>
<form action="Controller" method="post" id="postForm">
<label>Enter Test ID: </label>
<input type="text" name="test_id" value="">
<input type="submit" value="Submit" name="submitPost" class="btn btn-
primary">
</form>
</body>
</html>
我认为我的doPost方法或getParameter()调用的问题是在文本区域中包含文本之前尝试获取文本区域的参数。我也尝试了getAttribute(“textArea”),它也没有用。任何帮助表示赞赏!
答案 0 :(得分:0)
已解决:我所要做的就是将我的文本区域从我的get方法移动到jsp中的post方法,并且它有效。
<center><h1>Test Cases</h1></center>
<form action="Controller" method="get" id="getForm">
<label>Input Test Case ID #: </label>
<input type="text" name="input" value="">
<input type="submit" value="Submit" name="submitGet" class="btn btn-
primary"> <br><br><br><br>
</form>
<form action="Controller" method="post" id="postForm">
<label>Test Case Test Script: </label>
<textarea name="textArea" id="output">
<%=request.getAttribute("Local")%></textarea>
<label>Enter Test ID: </label>
<input type="text" name="test_id" value="">
<input type="submit" value="Submit" name="submitPost" class="btn btn-
primary">
</form>
</body>
</html>