简体   繁体   English

如何将值从jsp传递到java类

[英]how to pass values from jsp to java class

My Jsp page 我的Jsp页面

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="application/xml" trimDirectiveWhitespaces="true" %>
<%@ page import="com.raos.kookooivr.CallStatusValues" %>

<c:choose>

    <c:when test='${(param.event == "Conference" || param.event == "Hangup" || param.event == "Disconnect") && requestScope.state == "conferenceStarted"}'>
        <%     
            String session_id = request.getParameter("sid");
            out.print(CallStatusValues.getsessionid(session_id));
            String called_no = request.getParameter("cid");
            out.print(CallStatusValues.getcalledno(called_no));
            String actualtime = request.getParameter("total_call_duration");
            out.print(CallStatusValues.getactualtime(actualtime));
            String start_time = request.getParameter("start_time");
            out.print(CallStatusValues.getstarttime(start_time));
            String end_time = request.getParameter("time");
            out.print(CallStatusValues.getendtime(end_time));
        %>
    </c:when>
</c:choose>

My Java Class 我的Java课

package com.raos.kookooivr;

public class CallStatusValues
{
    public static String getsessionid(String session_id) 
    { 
        System.out.println(session_id);
        return session_id;
    }

    public static String getcalledno(String called_no) 
    { 
        System.out.println(called_no);
        return called_no; 
    }

    public static String getactualtime(String actualtime) 
    { 
        System.out.println(actualtime);
        return actualtime;
    }

    public static String getstarttime(String start_time) 
    { 
        System.out.println(start_time);
        return start_time;
    }

    public static String getendtime(String end_time) 
    { 
        System.out.println(end_time);
        return end_time;
    }
}

I am getting 500 Error. 我收到500错误。

I am trying to pass the values of " String session_id = request.getParameter("sid"); " to my java class and trying to print the session id 我试图将“ String session_id = request.getParameter("sid"); “的值传递给我的java类,并尝试打印会话ID

public static String getsessionid(String session_id) 
        { 
            System.out.println(session_id);
            return session_id;
        }

I need help to get the values from jsp to be printed using my java class. 我需要帮助来从jsp获取要使用我的java类进行打印的值。 Example my sid is 123456. Then, that session id should be passed from my jsp file to my java class. 例如,我的s​​id是123456。然后,该会话ID应该从我的jsp文件传递到我的java类。

I'm afraid there are multiple problems with your code. 恐怕您的代码有多个问题。

  • You can't use static methods - web servlets are multi-threaded, and need model class instances per thread (which usually maps to an instance per web session). 您不能使用静态方法-Web Servlet是多线程的,并且每个线程都需要模型类实例(通常每个Web会话都映射到一个实例)。
  • Your naming conventions are non-standard. 您的命名约定是非标准的。 The correct name for your session_id variable is sessionId and the getter should be getSessionId() - note the capitalization. session_id变量的正确名称是sessionId,而getter应该是getSessionId()-注意大小写。 This meets the Java Bean naming conventions. 这符合Java Bean命名约定。
  • The way you are coupling code to the model class (which incidentally has nowhere to store the data) is non-standard. 将代码耦合到模型类的方式(顺便说说,该地方无处存储数据)是不标准的。
  • Writing java code in a JSP is generally regarded as code smell for new projects. 在JSP中编写Java代码通常被认为是新项目的代码味道。 It is more normal to just use EL and tags in the JSP and move any Java code out into the model and controller. 仅在JSP中使用EL和标签并将任何Java代码移至模型和控制器中是更正常的。

I suggest you download a simple working J2EE JSP servlet project and analyse how it works. 我建议您下载一个简单的可运行的J2EE JSP servlet项目并分析其工作方式。 Then start to modify it to suit your requirements. 然后开始修改它以适合您的要求。 Something like http://crunchify.com/servlet-tutorial-getting-starting-with-jsp-servlet-example should get you started. http://crunchify.com/servlet-tutorial-getting-starting-with-jsp-servlet-example之类的东西会帮助您入门。

Unsure that you are doing what you want to do. 不确定您在做什么。

Here is what actually happens : 这是实际发生的情况:

  • a client sends a request that ends in calling the JSP page 客户端发送一个以调用JSP页面结尾的请求
  • the servlet container calls the JSP with the context of the calling request servlet容器使用调用请求的上下文来调用JSP
  • the JSP runs server side with the context of the calling request meaning that: JSP通过调用请求的上下文运行服务器端,这意味着:
    • nothing has been rendered at that time 当时什么都没有呈现
    • all the parameters "sid", "cid", etc. are those that the caller passed 所有参数“ sid”,“ cid”等都是调用者传递的参数
  • if any of the required parameters were not passed in calling request, you get a NullPointerException that ends in an Error 500 如果在调用请求中未传递任何必需的参数,则会得到一个NullPointerException,该错误以Error 500结尾

BTW using a class with static methods that way from a JSP, is at least uncommon... It could make sense if you need to be able to to complex computations inside the JSP, but in that case, you'd better use a servlet to do the processing, store result in request attribute and forward to the servlet. BTW在JSP中使用带有静态方法的类至少是不常见的……如果您需要能够在JSP内部进行复杂的计算,这可能是有道理的,但是在这种情况下,最好使用servlet进行处理,将结果存储在request属性中并转发到servlet。

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

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