如何将参数从jsp传递给java类?

时间:2015-12-06 08:08:36

标签: java jsp

我正在使用Tomcat8 WebSocket构建聊天室,代码来自tomcat的示例。但我不知道如何将我的变量传递给java类,这是我的代码:

public class ChatAnnotation {

    private static final Log log = LogFactory.getLog(ChatAnnotation.class);
    private static final Set<ChatAnnotation> connections =
            new CopyOnWriteArraySet<>();

    private final String player;

    private String username;
    private Session session;

    public ChatAnnotation() {
        player = "";
    }
}

我想将变量名放入以下java类中,在分配给播放器的构造函数ChatAnnotation()中:

-- -----------------------------------------------------
-- Table `PlayerStats`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `PlayerStats` (
  `PlayerUID` VARCHAR(32) NOT NULL,
  `LastModified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `PlayerKills` INT UNSIGNED NOT NULL DEFAULT 0,
  `AIKills` INT UNSIGNED NOT NULL DEFAULT 0,
  `TeamKills` INT UNSIGNED NOT NULL DEFAULT 0,
  `DeathCount` INT UNSIGNED NOT NULL DEFAULT 0,
  `ReviveCount` INT UNSIGNED NOT NULL DEFAULT 0,
  `CaptureCount` INT UNSIGNED NOT NULL DEFAULT 0,
  UNIQUE INDEX `idx_PlayerStats_uniquePlayer` (`PlayerUID` ASC),
  CONSTRAINT `fk_PlayerStats_PlayerInfo`
    FOREIGN KEY (`PlayerUID`)
    REFERENCES `PlayerInfo` (`UID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `PlayerInfo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `PlayerInfo` (
  `UID` VARCHAR(32) NOT NULL,
  `CreationDate` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  `Name` VARCHAR(256) CHARACTER SET 'utf8' NULL,
  `LastSide` VARCHAR(32) NULL,
  `BankMoney` FLOAT NOT NULL DEFAULT 0,
  `BattlEyeGUID` VARCHAR(32) NULL,
  PRIMARY KEY (`UID`))
ENGINE = InnoDB;

3 个答案:

答案 0 :(得分:1)

首先,如果你想调用ChatAnnotation的Constructor并更改播放器名称,那么你必须制作参数化的构造函数。

public ChatAnnotation(String player) {
    this.player = player;
}

现在要调用它,你必须在JSP页面中创建对象并将值传递给构造函数。

<%
    String name = (String) session.getAttribute("realname");
    ChatAnnotation chat = new ChatAnnotation(name);
%>

答案 1 :(得分:1)

所有的拳头使一个构造函数+ public getPlayer()方法成为ChatAnnotation类,

ChatAnnotation(String name){
    player  = name;
}
public String getPlayer(){
  return player;
}

你.jsp页面同样如此,

<%
String name = (String) session.getAttribute("realname");
ChatAnnotation chatAnnotation = new ChatAnnotation(name);
session.setAttribute("chatAnno",chatAnnotation);
%>  

在你要使用它的另一端(otherFile.jsp),从会话中拉回来,就像你说的那样,

<%
ChatAnnotation chatAnno = (ChatAnnotation)session.getAttribute("chatAnno");
String playerName = chatAnno.getPlayer();
%>

答案 2 :(得分:0)

在你的文件中写jsp:

 <jsp:useBean id="chatannotation" scope="application"
class="websocket.chat.ChatAnnotation" />

    <%
    String name = (String) session.getAttribute("realname");


    //-------now pass parameter "name" to your ChatAnnotation java file
   ChatAnnotation chatAnnotation = new ChatAnnotation(name);
   session.setAttribute("name",chatAnnotation);
    %> 

在你的班级java中这样做:

  ChatAnnotation chatAnnotation = (ChatAnnotation)session.getAttribute("name");
 String name = chatAnno.getName();