java - How can I make a link redirect to a servlet? -
this question has answer here:
- call servlet on click of hyperlink 4 answers
i have html file following link:
<li><a class="active" href="personalinfooutput.java">view personal information</a></li>
when clicks on "view personal information", want redirect servlet, why put:
href="personalinfooutput.java
but not working. returning:
http status 404 - /payroll/personalinfooutput.java
here personalinfooutput.java servlet
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class personalinfooutput extends httpservlet { protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); httpsession session = request.getsession(false); string employeeid = ""; if (session != null) { employeeid = (string) session.getattribute("employeeid"); } boolean st = false; try { class.forname("com.mysql.jdbc.driver").newinstance(); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/payroll_system", "root", ""); preparedstatement ps = con.preparestatement( "select employeeid, firstname, lastname, admin, dob, address, email, hourlyrate, gender, aleavebalance, sleavebalance, activestatus, role, bsb, bankname, accnumber, supernumber, supercompany payroll_system.employee_info employeeid = ?"); ps.setstring(1, employeeid); resultset rs = ps.executequery(); st = rs.next(); if (st) { boolean admintrue = rs.getboolean("admin"); boolean activetrue = rs.getboolean("activestatus"); out.println("<html>"); out.println("<head>"); out.println("<style>"); out.println( "table { border-collapse: collapse; width: 50%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even){background-color: #f2f2f2}"); out.println("tr:hover {background-color: #e2f4ff;}"); out.println("</style>"); out.println("<link rel = stylesheet type = text/css href = main.css>"); out.println("<title>personal information</title>"); out.print("</head>"); out.println("<body>"); out.println("<h1>personal information</h1>"); out.println("<table border =1>"); out.println("<tr>"); out.println("<td>name</td>"); out.println("<td>" + rs.getstring("firstname") + " " + rs.getstring("lastname") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>employee id</td>"); out.println("<td>" + rs.getstring("employeeid") + "</td>"); out.println("</tr>"); if (admintrue) { out.println("<tr>"); out.println("<td>admin</td>"); out.println("<td>yes</td>"); out.println("</tr>"); } else { out.println("<tr>"); out.println("<td>admin</td>"); out.println("<td>no</td>"); out.println("</tr>"); } out.println("<tr>"); out.println("<td>date of birth</td>"); out.println("<td>" + rs.getstring("dob") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>residential address</td>"); out.println("<td>" + rs.getstring("address") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>email</td>"); out.println("<td>" + rs.getstring("email") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>hourly income</td>"); out.println("<td>" + "$" + rs.getstring("hourlyrate") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>gender</td>"); out.println("<td>" + rs.getstring("gender") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>annual leave balance</td>"); out.println("<td>" + rs.getstring("aleavebalance") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>sick leave balance</td>"); out.println("<td>" + rs.getstring("sleavebalance") + "</td>"); out.println("</tr>"); if (activetrue) { out.println("<tr>"); out.println("<td>currently active</td>"); out.println("<td>yes</td>"); out.println("</tr>"); } else { out.println("<tr>"); out.println("<td>currently active</td>"); out.println("<td>no</td>"); out.println("</tr>"); } out.println("<tr>"); out.println("<td>role</td>"); out.println("<td>" + rs.getstring("role") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>bsb</td>"); out.println("<td>" + rs.getstring("bsb") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>bank name</td>"); out.println("<td>" + rs.getstring("bankname") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>bank account number</td>"); out.println("<td>" + rs.getstring("accnumber") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>superannuation company</td>"); out.println("<td>" + rs.getstring("supercompany") + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>superannuation number</td>"); out.println("<td>" + rs.getstring("supernumber") + "</td>"); out.println("</tr>"); out.println("</table>"); out.println("</body>"); out.println("</html>"); } } catch (exception e) { e.printstacktrace(); } out.close(); } }
long , winding road waits you.. first of all, if want call server, should have request mapping in method, example: /personal/infooutput/
, need pass link html code:
<li><a class="active" href="/personal/infooutput/">view personal information</a></li>
or call servlet:
<li><a class="active" href="personalinfooutput">view personal information</a></li>
currently not have http method, http post, need add too. https status 404 means server didn't find mapping/link. here tutorial: http://tutorials.jenkov.com/java-servlets/index.html
Comments
Post a Comment