如何在单击提交按钮时使用jsp将此表数据存储在会话中?

时间:2014-06-12 02:30:20

标签: jsp session

这是我的jsp页面,如何将所有值存储在会话中供以后使用。

out.println("<table>");
out.println("<tr><td>Movie Name :</td><td ><input type='text' name='movieName' id='movieName' </td></tr>");
out.println("<tr><td >Movie Category:</td><td ><input type='text' name='category' id='category'></td></tr>");
out.println("<tr><td >CD Cost:</td><td ><input type='text' name='cost' id='cost'></td></tr>");
out.println("<tr><td>Release Date:</td><td ><input type='text' name='release' id='release'></td></tr>");
out.println("<tr><td><input type='button' value='Submit' onclick='ValidateAddMovieDetails()'</td></tr>");
out.println("</table>");`'

有人可以指导我怎么做吗?

3 个答案:

答案 0 :(得分:2)

1)

out.println("<form id='form1' method='post' action='nextServlet'>");
out.println("<table>");
out.println("<tr><td>Movie Name :</td><td ><input type='text' name='movieName' id='movieName' </td></tr>");
out.println("<tr><td >Movie Category:</td><td ><input type='text' name='category' id='category'></td></tr>");
out.println("<tr><td >CD Cost:</td><td ><input type='text' name='cost' id='cost'></td></tr>");
out.println("<tr><td>Release Date:</td><td ><input type='text' name='release' id='release'></td></tr>");
out.println("<tr><td><input type='button' value='Submit' onclick='ValidateAddMovieDetails()'</td></tr>");
out.println("</table>");
out.println("</form>");

2)--------------------- Java Script ---------------

function ValidateAddMovieDetails(){

 //validate

 document.getElementById('form1').submit();
}

3)
----------------- nextServlet -----------

 String movieName=request.getParameter("movieName");
 String category=request.getParameter("category");

 session.setAttribute("movieName",movieName);
 session.setAttribute("category",category);

======================= OR ================ 3)

Movie.java

  public class Movie {

private String movieName;
private String category;
private double cost;
private String release;

public Movie(String movieName, String category, double cost, String release) {
    this.movieName = movieName;
    this.category = category;
    this.cost = cost;
    this.release = release;
}

public String getMovieName() {
    return movieName;
}

public void setMovieName(String movieName) {
    this.movieName = movieName;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public double getCost() {
    return cost;
}

public void setCost(double cost) {
    this.cost = cost;
}

public String getRelease() {
    return release;
}

public void setRelease(String release) {
    this.release = release;
}    
}

----------------- nextServlet -----------

 String movieName=request.getParameter("movieName");
 String category=request.getParameter("category");
 String cost=request.getParameter("cost");
 String release=request.getParameter("release");
  double costd= Double.parseDouble(cost);
Movie mov = new Movie(movieName, category, costd, release);
session.setAttribute("movie",mov);

获取

Movie movie = (Movie)session.getAttribute("movie");

String movieName = movie.getMovieName();
String category= movie.getMovieName();
double cost = movie.getCost();
...

答案 1 :(得分:1)

<强> HTML

<input type="hidden" name="myvar" id="hiddenField">

<强>的Javascript

document.getElementById("hiddenField").value=yourvar;

现在在下一页上,您可以获取此隐藏变量,并可以轻松地将其存储到会话变量中,如下所示

Second_page
JSP

<%
    String s=request.getParameter("myvar");
    session.setAttribute("sessionname",s);
%>

答案 2 :(得分:0)

在提交jsp之前,调用一个javascript函数,在其中取所有字段:值对并准备一个json字符串并将其发布到服务器。在服务器上获取一个模态对象,用json字符串中的表单值填充它,这可以使用一些第三方JSON库轻松完成。现在,您可以将此模态对象放入会话中以供将来使用。

如果你不想用json方式,那么只需将表单发布到服务器上,然后在servlet中使用request.getParameter()获取所有字段值,填充所有模态字段然后放入最终字段未来对象的会话中的模态对象。