登录Servlet- Login.java
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String pass = request.getParameter("pass");
HttpSession session = request.getSession();
if(Validate.checkUser(username, pass))
{
RequestDispatcher rs = request.getRequestDispatcher("Welcome");
rs.forward(request, response);
session.setAttribute("username", username);
response.sendRedirect("http://localhost:8080/login2/dietsection.html");
}
JSP Page-dietjsp.jsp
<%
String username=request.getParameter("username");
session.getAttribute(username);
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb",
"root","");
PreparedStatement ps=con.prepareStatement
("insert into diet values(?)");
ps.setString(1, username);
int i=ps.executeUpdate();
%>
答案 0 :(得分:0)
在JSP Page-dietjsp.jsp中使用引号
session.getAttribute("username");