2017-02-28 417 views
1

在netbeans中創建一個web服務器與我有3個文件index.jsp,response.jsp和client.java。 的想法是創建一個溫度轉換器,但只需要輸入,而不是做計算器的工作。 請任何幫助!?java類調用/調用jsp文件

的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(); 
    } 
+0

您需要將請求轉發到從JSP模型的邏輯,再往前一個響應頁面。查看「模型 - 視圖 - 控制器」(MVC),特別是舊的「模型2」架構。一分鐘或三次搜索後,我發現這些鏈接: https://en.wikipedia.org/wiki/JSP_model_2_architecture http://www.javaworld.com/article/2076557/java-web-development/understanding-javaserver -pages-model-2-architecture.html –

回答

0

你在你的代碼的多個問題。下面是一些建議:

  1. 比賽中index.htmlSortClient.java輸入名稱,以避免混淆和簡化屬性分配
  2. 所有字段創建setter和getter否則jsp:setPropertyjsp:getProperty將無法​​正常工作
  3. 講究對您的數據類型:String沒有nextDouble()。如果你還沒有請使用IDE(Eclipse,Netbeans,Intellij等)的幫助。
  4. 刪除不必要的庫和代碼,例如。 org.tempuri.*

下面是實現上述建議的代碼:

的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/

+0

感謝您的幫助!所以我還沒有學會解析,我的老師告訴我使用3套和1個吸氣劑!? –

+0

如果你不在'response.jsp'中顯示輸入(通過調用'jsp:getProperty' ) –