/ * *要更改此模板,请选择“工具”|模板 *并在编辑器中打开模板。 * /
package Registration;
import Pojos.Users;
import Pojos.hiber;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author parag
*/
@WebServlet(name="Reg",urlPatterns={"/Reg"})
public class Reg extends HttpServlet{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Registration </title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Registration</h1>");
out.println("</body>");
out.println("</html>");
}
finally{
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
String uname , fname ,lname, pwd , email , address , country , gender , hobby , education,image;
uname = request.getParameter("user_name");
fname = request.getParameter("fname");
lname = request.getParameter("lname");
pwd = request.getParameter("pass");
String p = request.getParameter("phone");
email =request.getParameter("em");
address = request.getParameter("add");
country = request.getParameter("country");
gender = request.getParameter("gender");
hobby = request.getParameter("hobby");
education = request.getParameter("edu");
String d = request.getParameter("sltdate");
String m = request.getParameter("sltmonth");
String y = request.getParameter("sltyear");
String bdate = d+"--"+m+"--"+y;
try{
SessionFactory sessionFactory = hiber.getSessionFactory();
Session session = sessionFactory.openSession();
Users user = new Users();
user.setUserName(uname);
user.setFirstName(fname);
user.setLastName(lname);
user.setPassword(pwd);
user.setPhoneNo(p);
user.setProfPic(null);
user.setEmail(email);
user.setAddress(address);
user.setBirthdate(bdate);
user.setCountry(country);
user.setGender(gender);
user.setHobby(hobby);
user.setEducation(education);
session.save(user);
Transaction transaction = session.beginTransaction();
if (true){
transaction.commit();
HttpSession session1 = request.getSession(true);
session1.setAttribute("un", uname);
response.sendRedirect("image_upload.jsp");
}
out.println("okkkk");
session.close();
//response.sendRedirect("index.jsp");
}catch(Exception e){
out.println("Error" +e.getMessage());
}
}
}
这是我定义了http会话变量的servlet,但在其他页面上我没有得到这个会话变量。怎么办?
答案 0 :(得分:0)
要检索存储在jsp中会话中的对象(如果它们是字符串,则可以执行此操作)。
<input type="text" value="${sessionScope.un}" />
OR
<% String username = (String)request.getSession().getAttribute("un"); %>
另外,创建HibernateFactory是一个非常昂贵的过程,考虑不要在每个请求中创建。