0

更新:我忘了添加我的GotApp.java

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GotApp extends HttpServlet {
  static DatabaseConn connection = null;
  static UserStore    dataStore  = null;
  static TaskCoderWtc coder      = null;
  ServletContext servletContext = null;
  static String       coderApp   = null;
  static String       workDir    = null;

  public void init() {
  try {
    servletContext = getServletContext();
    coderApp       = getParameter("tricon.proxy"); //exists in web.xml
    workDir        = getParameter("data.dir"); //exists in web.xml
    dataStore      = new UserStore(workDir); //exists in web.xml
    }
  }

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws IOException, ServletException {
  doPost(req, resp);
  }

  public UserStore getDataStore() {
  return dataStore;
  }
  public DatabaseConn getConnection() { // FIXME This needs a better name.
  return connection;
  }

  String getParameter(String str) throws Exception {
  if (servletContext == null) {
    throw new Exception("servletContext is null.");
    return servletContext.getInitParameter(str);
  }

  public User getUser(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException {
  // We only really need req.
  return dataStore.loadUser(getUsername(req));
  }

  // Will never return null.
  public Task getTask(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    User user = getUser(req, resp);
    //if (user == null) {
    //  return null;
    //}
    String taskname = req.getParameter("taskname");
    Task task = new Task(taskname);
    // Make a quick check here.
    if (task.isCoding()) {
      resp.sendRedirect("task.jsp?" + req.getQueryString());
    }
    user.addTask(task);
    return task;
  }

  public String getAction(HttpServletRequest req) {
  String action = req.getParameter("action");
  if (action == null) {
    action = "";
   }
  return action.toLowerCase();
  }

我有一个网页,要求用户选择 4 个选项,这些选项将存储为与选择相对应的字符串。这位于我的htmltask.java中:

import com.nav.wtc.model.coder.TaskRunner;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import helper.DatabaseConn;
import helper.UserStore;
import pages.GotApp;

public class HtmlTask extends GotApp {
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
    super.doPost(req, resp);

    UserStore    dataStore  = getDataStore();
    DatabaseConn connection = getConnection();
    TaskRunner   runner     = getTaskRunner();

    Task   task       = getTask(req, resp);
    String action     = getAction(req);

    resp.getWriter().println(showPage(task));
}

    public String showPage(Task task) {
      StringBufn sb   = new StringBufn();
      sb.add("<html>");
      sb.add("<body>");
      sb.add(showCall(task));
      sb.add("</body>
      sb.add("</html>
      return sb.toString();
    }
    private String showCall(Task task) {
    StringBufn sb = new StringBufn();
    sb.add("<div class='area'>");
    sb.add("<form method='get' action='task.jsp'>");
    sb.add("  <table>");
    sb.add("    <tr class='1'>");
    sb.add("      <td>");
    sb.add(" What level would you like to code to: <br> ");
    sb.add("  <input type ='radio' name='call' value='valsel'> Selections<br> ");
    sb.add(" <input type ='radio' name='call' value='valselpbd'> Parts<br> ");
    sb.add("  <input type ='radio' name='call' value='valselhbd'> Holes<br> ");
    sb.add("  <input type ='radio' name='call' value='valselrbd'> C-BOMs<br> ");
    sb.add("  <input type ='radio' name='call' value='valselpbdrbd'> BOMs<br> ");
    sb.add("       <input type='submit' value='Run Task' />");
    sb.add("      </td>");
    sb.add("    </tr>");
    sb.add("  </table>");
    sb.add("</form>");
    sb.add("</div>");
    return sb.toString();
  }

我想在这里取其中一个值(将由用户提交)并将其用作另一个名为TaskCoderImpl.java的 java 类文件中的输入:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileInputStream; // For debugging.
import java.io.FileOutputStream; // For debugging.
import java.io.OutputStream; // For debugging.
import java.io.InputStream; // For debugging.

void coder(File exe, File ord, File cod, File msg, File rls, String tcc )
        throws ProcessException {
    String[] cmd = {
            exe.getPath(),  // Executable
            ord.getPath(),  // InputOrder      (IN)
            cod.getPath(),  // ResultsCodedOrder (OUT)
            msg.getPath(),  // Messages   (OUT)
            rls.getPath(),  // Releases   (IN)
            tcc = ""
    };

Order codeOrder(File taskdir, Order order) throws ProcessException {
    String ordername = order.getName();

    // Create temp 'input' files.
    File orderFile   = createTriconOrderFile(taskdir, order);
    File releaseFile = createTriconReleaseFile(taskdir, order);

    // Define temp 'output' files.
    File messageFile = new File(taskdir, ordername + msgExt);
    File codedFile   = new File(taskdir, ordername + codExt);

    // Code order.
    coder(exe,
            orderFile,
            codedFile,
            messageFile,
            releaseFile,
            "USER 'value' INPUT");
return codedOrder;
}

我是否可以使用 htmltask.java 中的“值”(其中包含 servlet,即 req,resp 的东西)并将这些“值”用作我的 TaskCoderImpl.java 中的输入,因为它只需要一个字符串......如果是这样,我是否遗漏了什么……我真的希望我不会让任何人感到困惑……

更新:添加task.jsp

<%getServletContext().getRequestDispatcher("/Task").forward(request, response);%>

添加web.xml servlet 样式

<servlet>
<servlet-name>Task</servlet-name>
<servlet-class>pages.html.HtmlTask</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Task</servlet-name>
<url-pattern>/Task</url-pattern>
</servlet-mapping>
4

1 回答 1

0

那么task.jsp 只是一个java jsp 文件。该请求是下一页可用的隐式对象。

因此,它将获得 html 表单提交属性,您可以查看这些属性并使用它们来做任何事情。

<%=request.getParameter("call")%>

我认为您的参数是“呼叫”?打印整个请求并挑选出您需要的部分。

你也可以在EL里做。

你好,${param.call}

通过不使用 EL、jsf 和或其他更现代的东西,你正在使这个负载变得更加困难。输入框等有标签库。

于 2013-09-13T19:35:10.640 回答