java类调用/调用jsp文件

时间:2017-02-28 23:15:01

标签: java jsp constructor

在netbeans中创建一个webserver,我有3个文件index.jsp,response.jsp和client.java。 我们的想法是创建一个温度转换器,但只接受输入而不进行计算器工作。 请任何帮助!?

的index.jsp

<form name="Input Form" id="ftemp" action="response.jsp">
            <input type="text" name="temp" />
            <select name="conv1">
                <option>Celsius</option>
                <option>Fahrenheit</option>
            </select>
            <select name="conv2">
                <option>Fahrenheit</option>
                <option>Celsius</option>
            </select>

            <input type="submit" value="Submit" />
        </form>

的response.jsp

<body>

        <h1>your list is in order</h1>
        <jsp:useBean id="sortbean" scope="session" class="sortclient.SortClient" />
        <jsp:setProperty name="sortbean" property="input" />          
        <jsp:setProperty name="sortbean" property="cel"  />
        <jsp:setProperty name="sortbean" property="fahr" />
        <jsp:getProperty name="sortbean" property="input" />
    </body>

client.java

public class SortClient {

    private String input;   
    double cel = 0;
    double fahr = 0;

        public SortClient (){
            input = null;
        }

    public String getInput() {

    try{
        String key = getKey();
        input = mergeSort (input,key);
        double tempCelsius = input.nextDouble();
        double tempFahrenheit = input.nextDouble();

        return input;
    }catch (Exception ex){
            System.out.println(ex); //we would log this
            return "That is not a valid list";
        }
    }



    public void setInput(String input) {
        this.input = input;

    }

   public double toCelsius( double tempFahrenheit )
    { 
        return ((5.0 / 9.0) * ( tempFahrenheit - 32 ));

    }

   public double toFahrenheit( double tempCelsius )
    { 
        return (tempCelsius * 9.0 / 5.0) + 32;

    }


    private static String mergeSort(java.lang.String input, java.lang.String userKey) {
        org.tempuri.Service service = new org.tempuri.Service();
        org.tempuri.IService port = service.getBasicHttpBindingIService();
        return port.mergeSort(input, userKey);
    }

    private static String getKey() {
        org.tempuri.Service service = new org.tempuri.Service();
        org.tempuri.IService port = service.getBasicHttpBindingIService();
        return port.getKey();
    }

1 个答案:

答案 0 :(得分:0)

您的代码中存在多个问题。以下是一些建议:

  1. 匹配index.htmlSortClient.java中的输入名称,以避免混淆并简化属性分配
  2. 为所有字段创建setter和getter,否则jsp:setPropertyjsp:getProperty将无效
  3. 注意您的数据类型:String没有nextDouble()。如果您还没有请使用IDE(Eclipse,Netbeans,Intellij .etc)帮助。
  4. 删除不必要的库和代码,例如。 org.tempuri.*
  5. 以下是实施上述建议的代码:

    <强>的index.jsp

      <form name="Input Form" id="ftemp" action="response.jsp">
        <input type="text" name="temp" />
        <select name="conv1">
          <option>Celsius</option>
          <option>Fahrenheit</option>
        </select>
        <select name="conv2">
          <option>Fahrenheit</option>
          <option>Celsius</option>
        </select>
        <input type="submit" value="Submit" />
      </form>
    

    <强>的response.jsp

    <h1>your list is in order</h1>
    <jsp:useBean id="sortbean" scope="session" class="sortclient.SortClient" />
    <jsp:setProperty name="sortbean" property="*" />
    Input: <jsp:getProperty name="sortbean" property="temp" /><br>
    Conv1: <jsp:getProperty name="sortbean" property="conv1" /><br>
    Conv2: <jsp:getProperty name="sortbean" property="conv2" /><br>
    Result: <jsp:getProperty name="sortbean" property="result" />
    

    <强> SortClient.java

    package sortclient;
    
    public class SortClient {
        private String temp;
        private String conv1;
        private String conv2;
        private Double result;
    
        public String getTemp() {
            return temp;
        }
    
        public void setTemp(String temp) {
            this.temp = temp;
        }
    
        public String getConv1() {
            return conv1;
        }
    
        public void setConv1(String conv1) {
            this.conv1 = conv1;
        }
    
        public String getConv2() {
            return conv2;
        }
    
        public void setConv2(String conv2) {
            this.conv2 = conv2;
        }
    
        public Double getResult() {
            result = Double.parseDouble(temp);
            if (conv1.equalsIgnoreCase(conv2)) {
                return result;
            } else if (conv2.equalsIgnoreCase("Celsius")) {
                return toCelsius(result);
            } else if (conv2.equalsIgnoreCase("Fahrenheit")) {
                return toFahrenheit(result);
            }
            return 0.0;
        }
    
        public double toCelsius(double tempFahrenheit )
        {
            return ((5.0 / 9.0) * ( tempFahrenheit - 32 ));
    
        }
    
        public double toFahrenheit( double tempCelsius )
        {
            return (tempCelsius * 9.0 / 5.0) + 32;
    
        }
    
    }
    

    有关详细指南,请参阅this tutorial

    正如其他人所说的那样,这种方法实际上被认为是过时的,因为它是紧密耦合的,并且不能大规模维护。所以我也建议你从教程中学习MVC模式,如:

    http://courses.coreservlets.com/Course-Materials/csajsp2.html http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/