[以按钮的形式单击时需要通过servlet调用Java函数

问题描述 投票:-1回答:1

我有一个带有提交按钮的简单表单(Ci.jsp),我需要通过servelt调用my(ui.java)。我需要一些帮助来将我的jsp和Java与servlet连接时感到困惑。在Ijava下面,添加了我的jsp和java。我已经提到了Stack Overflow,Git Hub等中大多数当前可用的解决方案。但是,这些解决方案都没有成功解决。

ci.jsp

<body>
    <form action="ciservice" method="GET">
        <input type="submit" value="submit">    

    </form>

</body>

Ui.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.ButtonGroup;
import javax.swing.table.DefaultTableModel;

public class UI extends javax.swing.JFrame {

     private javax.swing.JTable result;


    ArrayList<Integer> Ci = new ArrayList<Integer>();
    int totlComplexity = 0;
    int recursion = 0;
    int maxCases, testMaxCases = 0;
    String uploadedContent = "C:\\Users\\kusal\\Desktop\\kusal.txt";


    public void viewResult() throws FileNotFoundException {
       // readFile();
        DefaultTableModel model = (DefaultTableModel) result.getModel();

        Object[] CiObjs = Ci.toArray();

        model.addColumn("Ci", CiObjs);


    }

    public ArrayList<Integer> inheritanceCi() {

        int complexity = 0;
        String fileInput = uploadedContent.toString(); //read the txt document
        String keywords[] = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char",
            "const", "continue", "default", "do", "double", "enum", "final",
            "finally", "float", "for", "goto", "if", "instanceof", "int", "interface",
            "native", "new", "package", "short", "strictfp", "super",
            "switch", "synchronized", "this", "transient",
            "volatile", "while", "FileReader"};
        String ExceptionsSelection[] = {
            "ArithmeticException", "ArrayIndexOutOfBoundsException", "ClassNotFoundException", "FileNotFoundException", "IOException",
            "InterruptedException", "NoSuchFieldException", "NoSuchMethodException", "NullPointerException", "NumberFormatException",
            "RuntimeException", "StringIndexOutOfBoundsException"
        };

        String[] line = fileInput.split("\r\n|\r|\n");

        for (int i = 0; i < line.length; i++) {

            String withoutspace = line[i].trim();
            if (withoutspace.startsWith("/") || withoutspace.startsWith("*")) {//set lines with comments to 0 Cs
                complexity = complexity;
                System.out.println(complexity + " " + line[i]);
            }
            if (withoutspace.startsWith("import")) {

                complexity = complexity;
                System.out.println(complexity + " " + line[i]);
            }

            if (withoutspace.startsWith("try") || withoutspace.startsWith("else")) {

                complexity = 0;
                System.out.println(complexity + " " + line[i]);
            } else {
                String[] words = line[i].split("\\s+");
                for (int r = 0; r < words.length; r++) {

                    if (words[r].contains("public") && words[r + 1].contains("class")) {
                        complexity = complexity + 2;
                        System.out.println(complexity + " " + line[i]);

                        if (words[r].contains("extends") || words[r].contains("implements")) {
                            if (words[i].contains(",")) {
                                complexity = complexity + 1;
                                System.out.println(complexity + " " + line[i]);
                            }
                            complexity = complexity + 1;
                            System.out.println(complexity + " " + line[i]);
                        }

                    }

                    if (words[r].contains("}")) {

                        complexity = complexity;
                        System.out.println(complexity + " " + line[i]);

                    }
                    if (words[r].contains("System")) {// System and out selection complexity
                        complexity = complexity + 2;
                        System.out.println(complexity + " " + line[i]);
                    }
                    if (words[r].equals("static") && words[r + 1].equals("void")) {
                        complexity = complexity + 2;
                        System.out.println(complexity + " " + line[i]);
                    }

                    if (words[r].contains("return")) {
                        complexity = complexity + 2;
                        System.out.println(complexity + " " + line[i]);
                    }
                    if (words[r].contains("throw")) {//if throw e
                        if (words[r + 1].startsWith("e") && words[r + 1].endsWith(";")) {
                            complexity = complexity + 2;
                            System.out.println(complexity + " " + line[i]);
                        }
                        if (words[r].contains("//")) {
                            complexity = complexity + 2;
                            System.out.println(complexity + " " + line[i]);
                        }
                    }
                    for (int u = 0; u < keywords.length; u++) {

                        if (words[r].equals(keywords[u])) {
                            complexity = complexity + 2;
                            r++;
                            System.out.println(complexity + " " + line[i]);
                        }

                    }

                }

            }
            Ci.add(complexity);
            complexity = 0;
        }

        return Ci;
    }
}

非常感谢,任何建议将不胜感激。

java eclipse class servlets
1个回答
0
投票

[如果要将jsp页面连接到servlet,则应为servlet指定URL,并将该URL放在jsp表单的get或post方法中。看一个很好的例子,你可以看到this但您不应使用swing或javafx或任何与html不相关的gui。

© www.soinside.com 2019 - 2024. All rights reserved.