我有一个网络应用程序,我有不同的导航锚标签,如家庭,个人资料等.
我想要的是:
当我按下主页或个人资料等锚标签时.我只想确保当前用户在Tags/JSP页面中获取其信息.
我正在尝试的示例示例:
<a href="${pageContext.request.contextPath}/JSPAddress.jsp">Profile</a>
Run Code Online (Sandbox Code Playgroud) 我在Servlet中使用以下命令在Session中存储了用户ID:
HttpSession session = request.getSession();
session.setAttribute("user", user.getId());
Run Code Online (Sandbox Code Playgroud)
现在,我想从另一个Servlet访问该用户ID:
HttpSession session = request.getSession(false);
int userid = (int) session.getAttribute("user"); // This is not working
OR
User user = new User();
user.setId(session.getAttribute("user")); This ain't possible (Object != int)
Run Code Online (Sandbox Code Playgroud)
题:
对于多个图像检索,我PhotoHelperServlet使用锚标记调用a 来获取imageNames(多个图像),如下所示
PhotoHelperServlet 得到的名字 Images
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Getting userid from session
Image image = new Image();
image.setUserid(userid);
ImageDAO imageDAO = new ImageDAO();
try {
List<Image> imageId = imageDAO.listNames(image);
if (imageId == null) {
// check if imageId is retreived
}
request.setAttribute("imageId", imageId);
//Redirect it to home page
RequestDispatcher rd = request.getRequestDispatcher("/webplugin/jsp/profile/photos.jsp");
rd.forward(request, response);
catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
在ImageDAO listNames()方法中:
public List<Image> listNames(Image image) throws IllegalArgumentException, SQLException, ClassNotFoundException …Run Code Online (Sandbox Code Playgroud) 如果使用 Apache Commons Validator 会出现错误:
java.lang.NoClassDefFoundError: org/apache/oro/text/perl/Perl5Util
即使 Apache Commons Validator /dependencies 声明 ORO.jar 是可选的。
我使用JSP中的以下代码将生日月份存储在数据库中作为值.
<select name="birthday_month" id="birthday_month">
<option value="-1">Month</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
...
</select>
Run Code Online (Sandbox Code Playgroud)
在JSP中输出代码以使用我正在使用的JSTL显示以前选择的项目(这是不正确的)
<select name="birthday_month" id="birthday_month">
<c:forEach var="value" items="${birthdaymonth}">
<option value="${birthdaymonth}">${birthdaymonth}</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
...
</c:forEach>
</select>
Run Code Online (Sandbox Code Playgroud)
What I am getting from this code is value like 1 or 2 in select tag
其他信息:
request.setAttribute("birthdaymonth", user.getBirthdayMonth());我在期待什么
我的ImageDAO看起来像这样:
public InputStream getPhotos(Long userid) throws
IllegalArgumentException, SQLException, ClassNotFoundException {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultset = null;
Database database = new Database();
InputStream binaryStream = null;
try {
connection = database.openConnection();
preparedStatement = connection.prepareStatement(SQL_GET_PHOTO);
preparedStatement.setLong(1, userid);
preparedStatement.executeUpdate();
while(resultset.next()) {
binaryStream = resultset.getBinaryStream(4);
}
} catch (SQLException e) {
throw new SQLException(e);
} finally {
close(connection, preparedStatement, resultset);
}
return binaryStream;
}
Run Code Online (Sandbox Code Playgroud)
我的ImageServlet看起来像这样:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Getting user …Run Code Online (Sandbox Code Playgroud)