简体   繁体   English

使用JSP和Java的WebApp

[英]WebApp using JSP and Java

I am new to JSP and WebApplications and I encountering a problem. 我是JSP和WebApplications的新手,遇到问题。 I have look over many articles but did not manage to load data from a file using Java and displaying it on a webpage. 我看过许多文章,但没有设法使用Java从文件加载数据并将其显示在网页上。

I am reading data in the main function and displaying it with JSP. 我正在读取main函数中的数据,并使用JSP显示它。

Here is the code for the java class: 这是java类的代码:

package org.mypackage.hello;


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class NameHandler {
private String name;
private String type;
private String assignmentName;
private String moduleCode;
private String moduleName;
private String weight;
private String date;


public NameHandler() {
    name = name;
    type = null;
    assignmentName = null;
    moduleCode = null;
    moduleName = null;
    weight = null;
    date = null;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getAssignmentName() {
    return assignmentName;
}

public void setAssignmentName(String assignmentName) {
    this.assignmentName = assignmentName;
}

public String getModuleCode() {
    return moduleCode;
}

public void setModuleCode(String moduleCode) {
    this.moduleCode = moduleCode;
}

public String getModuleName() {
    return moduleName;
}

public void setModuleName(String moduleName) {
    this.moduleName = moduleName;
}

public String getWeight() {
    return weight;
}

public void setWeight(String weight) {
    this.weight = weight;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}




public static void main(String[] args) {

    NameHandler obj = new NameHandler();

    try (Scanner read = new Scanner (new File("file.txt"))) {
        read.useDelimiter(",");


        while(read.hasNext()){
            obj.setType(read.next());
            obj.setAssignmentName(read.next());
            obj.setModuleCode(read.next());
            obj.setModuleName(read.next());
            obj.setWeight(read.next());
            obj.setDate(read.next());


            System.out.println(obj.getType());
            System.out.println(obj.getAssignmentName());
            System.out.println(obj.getModuleCode());
            System.out.println(obj.getModuleName());
            System.out.println(obj.getWeight());
            System.out.println(obj.getDate());
            System.out.println("-------------------------");



        }
        read.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(NameHandler.class.getName()).log(Level.SEVERE, null, ex);
    }

}

} }

Here is the index html file: 这是索引html文件:

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <h1>Entry Form</h1>

    <form name="Name Input Form" action="response.jsp">
        Enter your name:
        <input type="text" name="name" />
        <input type="submit" value="OK" />
    </form>

</body>

Here is the jsp file: 这是jsp文件:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <jsp:useBean id="mybean" scope="session"     class="org.mypackage.hello.NameHandler" />
    <jsp:setProperty name="mybean" property="name" />
    <jsp:getProperty name="mybean" property="name" />


    <jsp:useBean id="stringBean" class="org.mypackage.hello.NameHandler" />


    <jsp:setProperty name="stringBean" property="assignmentName"  value="propertyValue" />

    <ol>
        <li><jsp:getProperty name="stringBean"   property="assignmentName" /></li>
        <li><jsp:getProperty name="stringBean" property="type" /></li>
        <li><jsp:getProperty name="stringBean" property="moduleCode" /></li>
        <li><jsp:getProperty name="stringBean" property="moduleName" /></li>
        <li><jsp:getProperty name="stringBean" property="weight" /></li>
        <li><jsp:getProperty name="stringBean" property="date" /></li>
    </ol>
 </body>

 </html>

I have seen that you can link the JSP file with the java classes but I do not really know if I can link it with the main thing. 我已经看到您可以将JSP文件与java类链接,但是我真的不知道是否可以将其与主要内容链接。 It keeps on printing null. 它继续打印null。

You have entered the code for setting values in the main method, which is not executed. 您已经在main方法中输入了用于设置值的代码,该代码未执行。 Move it, for example, to the NameHandler constructor. 例如,将其移动到NameHandler构造函数。 The right place for setting values, however, is in another class. 但是,设置值的正确位置在另一个类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM