我在alter.jsp
中提交值并将其置于servlet changeServlet.java
中。
alter.jsp -
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Department Creation</title>
<style>
a:link, a:visited {
background-color:darkolivegreen;
color: white;
padding: 10px 25px;
text-align: center;
width:100px;
text-decoration: none;
display: inline-block;
border-radius:20px;
}
a:hover, a:active {
background-color: lightgreen;
}
header {
background-color:teal;
color:white;
text-align:center;
padding:5px;
}
nav {
line-height:30px;
height:300px;
width:100px;
float:left;
padding:45px;
}
section {
height:270px;
width:550px;
float:right;
padding:150px;
}
footer {
background-color:black;
color:white;
clear:both;
text-align:center;
padding:5px;
}
#depart {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#depart td, #depart th {
border: 2px solid black;
text-align: left;
padding: 8px;
}
#depart tr:hover {background-color: #ddd;}
#depart th {
padding-top: 12px;
padding-bottom: 12px;
background-color: lightslategray;
color: white;
}
</style>
</head>
<body style="background-color:lightsteelblue;">
<%
String userName = null;
String sessionID = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("admin")) {
userName = cookie.getValue();
}
}
}
%>
<header>
<h1>File Tracking System</h1>
<div><span style="float:right">Hi <%=userName%></span></div>
<br>
<form style="float:right;" action=" LogoutServlet" method="post">
<input type="submit" value="Logout" >
</form>
<br>
</header>
<br>
<a href="department.jsp"><font color="white">1.Create Department</font></a>
<a href="c_user.jsp"><font color="white">2.Enter User Details</font></a>
<a href="seeuserdetails.jsp"><font color="white">3.See User Details</font></a>
<br>
这里我提交了值,action是changeServlet
<nav>
<form action="changeServlet" method="post">
<center>
<table id="depart">
<thead>
<tr>
<th colspan="2">Change user detail</th>
</tr>
</thead>
<tbody>
<tr>
<td>User Name :</td>
<td><input type="text" name="user" value="" size="50" /></td>
</tr>
<tr>
<td>Column Entry</td>
<td><select name="column">
<option>username</option>
<option>password</option>
<option>role</option>
<option>firstname</option>
<option>lastname</option>
<option>departmentname</option>
<option>email</option>
<option>mobile</option>
</select></td>
</tr>
<tr>
<td>New data:</td>
<td><input type="text" name="data" value="" size="50" /></td>
</tr>
</tbody>
</table>
</center>
<input type="reset" value="Clear" name="Clear" />
<input type="submit" value="Submit" name="Submit" />
</form>
</nav>
<section><img src="css/NSIC-logo1.png" width="537" height="267" alt="NSIC-logo1"/>
</section>
<br>
<footer>
Copyright 2016 NSIC. All right reserved.
</footer>
</body>
</html>
我在changeServlet.java
中获取了值,但它没有显示任何错误,并且没有被重定向到只是打开http://localhost:8080/test9/changeServlet
的任何位置,并且卡在这里显示一个白色的空白页
changeServlet.java
package bean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class changeServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//request.getRequestDispatcher("link.html").include(request, response);
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("JSESSIONID")){
System.out.println("JSESSIONID="+cookie.getValue());
break;
}
}
}
HttpSession session = request.getSession(false);
System.out.println("admin="+session.getAttribute("admin"));
if(session!=null && session.getAttribute("admin") != null){
String admin=(String)session.getAttribute("admin");
boolean status=false;
try{
String user=request.getParameter("user");
String column=request.getParameter("column");
String data=request.getParameter("data");
boolean checkc=checkchar.value(column);
boolean checkp=checkchar.value(data);
if语句在提交错误的条目时检查是否有错误的条目或空值,它会重定向到entry.jsp。这意味着它一直工作到这里,但在此之后不起作用。
if(checkc==true&&checkp==true) {
Connection con=ConnectionProvider.getCon();
String sql="update usertable set '"+column+"'='"+data+"' where username='"+user+"'";
PreparedStatement pstmt =con.prepareStatement(sql);
int rs=pstmt.executeUpdate();
if(rs>0){status=true;}
if(status){
response.sendRedirect("updated.jsp");
}
else
{
response.sendRedirect("notinsert.jsp");
}
}
else{response.sendRedirect("entry.jsp");}
}catch(SQLException e){}
}else{
RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.html");
PrintWriter out= response.getWriter();
out.println("<font color=red>Either user name or password is wrong.</font>");
rd.include(request, response);
}
}
}