Webページのテキスト領域からmy jspのテキストを変更しようとしています。変更されたテキストを取得してAPIを使用してWebサイトにプッシュできます。しかし、私がdoPostメソッドで.getParameter( "textArea")呼び出しを使用してテキスト領域から変更されたテキストを取得しようとするたびに、コンソールでそれを印刷しようとするとnullが出力されます。私は、Webサイトから必要な情報を取得し、テキストエリアに情報の価値を置くGETメソッドを持っています。私は私のポストメソッドをテストし、それはウェブサイトに投稿します。ここでjspのテキスト領域からテキストを取得しようとしました。nullを返しました。
は私のdoGetとdoPostメソッドのメソッドを持っている私のController.javaクラスである:ここで
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")も試してみましたが、どちらもうまくいきませんでした。どんな助けもありがとう!
メソッドをgetからpostに変更すると、ウェブサイトからプルしようとした情報がテキスト領域に表示されます。 –