Java 删除该属性。感谢您的支持answer@Siva我想你把范围弄糊涂了。您正在会话作用域中存储数据,并尝试从请求作用域获取数据。你也可以这样做,反之亦然。这意味着,若您不想更改jsp代码,那个么在servlet中将数据存储在会话范围内,但在使用数据之后,您必须
Java 删除该属性。感谢您的支持answer@Siva我想你把范围弄糊涂了。您正在会话作用域中存储数据,并尝试从请求作用域获取数据。你也可以这样做,反之亦然。这意味着,若您不想更改jsp代码,那个么在servlet中将数据存储在会话范围内,但在使用数据之后,您必须,java,jsp,servlets,Java,Jsp,Servlets,删除该属性。感谢您的支持answer@Siva我想你把范围弄糊涂了。您正在会话作用域中存储数据,并尝试从请求作用域获取数据。你也可以这样做,反之亦然。这意味着,若您不想更改jsp代码,那个么在servlet中将数据存储在会话范围内,但在使用数据之后,您必须使会话无效或删除该属性。 <!DOCTYPE HTML><%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding=
删除该属性。感谢您的支持answer@Siva我想你把范围弄糊涂了。您正在会话作用域中存储数据,并尝试从请求作用域获取数据。你也可以这样做,反之亦然。这意味着,若您不想更改jsp代码,那个么在servlet中将数据存储在会话范围内,但在使用数据之后,您必须使会话无效或删除该属性。
<!DOCTYPE HTML><%@page language="java"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>loginform</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<form name=loginform action=TestServlet method=post>
Input ID <input type=text name="id" size=10></input> <br><br>
<Input type="button" value=Retrive> <br>
<label for = "getdata" ></label> <br>
value is <%= request.getSession().getAttribute("data") %>
<input type=submit value=submit></input>
</form>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.*;
/**
* Servlet implementation class TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
// Actual logic goes here.
try {
String getvalue=request.getParameter("id");
PrintWriter out = response.getWriter();
out.println(getvalue);
out.println("attempting to read from table");
Class.forName("oracle.jdbc.driver.OracleDriver");
// Open a connection
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@IP:Port:ServiceName, UN, Pass);
// Execute SQL query
Statement stmt = conn.createStatement();
String sqlinsert, sqlselect;
sqlinsert = "Insert into abc(abc) values("+getvalue+")";
sqlselect="Select abc from abc";
ResultSet rs = stmt.executeQuery(sqlinsert);
ResultSet rs1 = stmt.executeQuery(sqlselect);
int id=0 ;
// Extract data from result set
while(rs1.next()){
//Retrieve by column name
id = rs1.getInt("ABC");
//Display values
out.println("ID: " + id + "<br>");
}
HttpSession session = request.getSession(false);
request.setAttribute("data", "0");
request.getRequestDispatcher("/loginform.jsp").forward(request, response);
// out.println("</body></html>");
// Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
}
}