Best practice of getting request parameters in JSP page

int period_Id = 0;
try
period_Id = Integer.parseInt(request.getParameter("period_id"));
catch(Exception e)
  period_Id = 0;
}This is the normal way we use to get parameters from previous page.
But I am not sure if it is thebest practice.
Please respond if anyone knows a better way.
Regards

Using java code in a JSP is a horrible practice. There are far better ways to write JSP's nowadays in the form of custom tags, JSTL and possibly JSF. Combined with servlets this will make your code clean, reusable and maintainable.
However if you insist and using scriptlets, I'd say create your own request bean that wraps around the HttpServletRequest and write methods getParameterInt(), getParameterDouble(), etc. Such a method could look like this:
public int getParameterInt(String name, int default)
String paramstr = request.getParameter(name);
if(paramstr == null || paramstr.length() == 0){
  return default;
try{
   return Integer.parseInt(paramstr);
} catch(Exception e) {
  return default;
}With such a bean you never have to do that exception catching yourself again: you can just pass the parameter name and a default value that will be returned if the parameter does not exist or is not a valid integer.

Similar Messages

  • SAP Best practice for Material request form

    Hai Sap gurus,
    Do we have any sap best practice for material request form? If so please help me to find this best practice provided by the SAP. I searched through sap help but i was unable to find one.
    Same way i also need to find the sap best practice for Change request form too...
    Thanking you all in advance.

    Hi,
    Check these links
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/2b50ac90-0201-0010-d597-8d833833f9e0
    and using the service market place link to down load the best practices
    http://www.sap.com/services/bysubject/servsuptech/index.epx

  • Urgent! pass parameters between jsp pages

    Does anyone can give an example about how to pass parameters between jsp pages?
    This is what I do:
    pageContext.forward("pag_loginc.jsp?p_idusr=" + strUsr + "&c_password=");
    But when deployed is not working.
    Help is needed.

    can you put that stuff in the session context in the first page, then pull it out in the second page?

  • Best Practice for Tranport request Naming

    Hi,
    We are using SolMan 4.0 during implementation of ECC 6.0.
    We have placed the blueprint and we are in configuration phase.
    We have a IMG project created in the DEV system and was assinged in Solution Manager project under  System Landscape->IMG Projects.
    Now that consultants are going to dev system and customizing they are creating their transport requests.
    Is there any best practice for the naming convention or the transport requests..
    By creating one IMG project for entire implementation is that going to create any problem..!!
    Please sgugest.
    Thanks & Regards
    Mrutyunjay

    As per MSFT best practices(Mentioned by Scott) keep it short as much as possible. You can use SP for SharePoint-SUBSite
    also check this blog for best practices.
    http://www.networkworld.com/community/blog/simple-naming-conventions-improve-end-user-experience-sharepoint-sites
    also one more thing you should consider, never use the reserved words into the SharePoint URLs. you will able to create the site/lis/library/folder but when you browse get the 404 errors.
    check this blog:
    http://www.sharepointblog.cz/2012/04/reserved-words-in-sharepoint-url.html
    http://techtrainingnotes.blogspot.com/2012/03/names-you-cant-use-for-sharepoint.html
    Please remember to mark your question as answered &Vote helpful,if this solves/helps your problem. ****************************************************************************************** Thanks -WS MCITP(SharePoint 2010, 2013) Blog: http://wscheema.com/blog

  • Unable get complete filepath from jsp page using request.getParameter()

    Hey all,
    i am actually trying to get the selected file path value using request.getParameter into the servlet where i will read the (csv or txt) file but i dont get the full path but only the file name(test.txt) not the complete path(C:\\Temp\\test.txt) i selected in JSP page using browse file.
    Output :
    FILE NAME : TEST
    FILE PATH : test.txt
    Error : java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at java.io.FileReader.<init>(FileReader.java:55)
    at com.test.TestServlet.processRequest(TestServlet.java:39)
    at com.test.TestServlet.doPost(TestServlet.java:75)
    JSP CODE:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>DEMO SERVLET</title>
    </script>
    </head>
    <body>
    <h2>Hello World!</h2>
    <form name="myform" action="TestServlet" method="POST"
    FILE NAME : <input type="text" name="filename" value="" size="25" /><br><br>
    FILE SELECT : <input type="file" name="myfile" value="" width="25" /><br><br>
    <input type="submit" value="Submit" name="submit" />
    </form>
    </body>
    </html>
    Servlet Code :
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, FileNotFoundException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
    String filename = request.getParameter("filename");
    out.println(filename);
    String filepath = request.getParameter("myfile");
    out.println(filepath);
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet TestServlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
    if (filepath != null) {
    File f = new File(filepath);
    BufferedReader br = new BufferedReader(new FileReader(f));
    String strLine;
    String[] tokens;
    while ((strLine = br.readLine()) != null) {
    tokens = strLine.split(",");
    for (int i = 0; i < tokens.length; i++) {
    out.println("<h1>Servlet TestServlet at " + tokens[i] + "</h1>");
    out.println("----------------");
    out.println("</body>");
    out.println("</html>");
    } finally {
    out.close();
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    Needed Output :
    FILE NAME : TEST
    FILE PATH : C:\\Temp\\test.txt
    Any suggestions Plz??

    As the [HTML specification|http://www.w3.org/TR/html401/interact/forms.html] states, you should be setting the enctype to multipart/form-data to be able to upload files to the server. In the server side, you need to process the multipart/form-data binary stream by parsing the HttpServletRequest#getInputStream(). It is a lot of work to get it to work flawlessly. There are 3rd party API's around which can do that for you, such as [Apache Commons FileUpload|http://commons.apache.org/fileupload] (carefully read the User Guide how to use it).
    You can also consider to use a Filter which makes use of the FileUpload API to preprocess the request so that you can continue writing the servlet code as usual. Here is an example: [http://balusc.blogspot.com/2007/11/multipartfilter.html].

  • In the Begining it's Flat Files - Best Practice for Getting Flat File Data

    I probably should have posed this question here before I delved into writing Java to get data for reports, but better late than never.
    Our ERP is written in COBOL. We have a third party ODBC which allows us to access data using a version of SQL. I have several Java sources compiled in my database that access the data and return something relevant. The Java sources are written in a procedural style rather than taking advantage of object oriented programming with attributes and methods.
    Now that I am becoming more comfortable with the Java language, I would greatly appreciate any feedback as to best practices for incorporating Java into my database.
    My guess is that it would be helpful to model the ERP "tables" with Java classes that would have attributes, which correspond to the fields, and methods to return the attributes in an appropriate way. Does that sound reasonable? If so, is there a way to automate the task of modeling the tables? If not reasonable, what would you recommend?
    Thanks,
    Gregory

    Brother wrote:
    I probably should have posed this question here before I delved into writing Java to get data for reports, but better late than never.
    Our ERP is written in COBOL. We have a third party ODBC which allows us to access data using a version of SQL. I have several Java sources compiled in my database that access the data and return something relevant. The Java sources are written in a procedural style rather than taking advantage of object oriented programming with attributes and methods.
    OO is a choice not a mandate. Using Java in a procedural way is certainly not ideal but given that it is existing code I would look more into whether is well written procedural code rather than looking at the lack of OO.
    Now that I am becoming more comfortable with the Java language, I would greatly appreciate any feedback as to best practices for incorporating Java into my database.
    My guess is that it would be helpful to model the ERP "tables" with Java classes that would have attributes, which correspond to the fields, and methods to return the attributes in an appropriate way. Does that sound reasonable? If so, is there a way to automate the task of modeling the tables? If not reasonable, what would you recommend?Normally you create a data model driven by business need. You then implement using whatever means seem expedient in terms of other business constraints to closely model that data model.
    It is often the case that there is a strong correlation between data models and tables but certainly in my experience it is rare when there are not other needs driven by the data model (such as how foreign keys and link tables are implemented and used.)

  • Best practice in getting compliance rates of Software Update Deployments

    Hi,
    Would like to ask around on how others generate reports about software update deployment compliance. What do you use to get this report? Are there best practices for gathering software update compliance reports?

    There is not really a best-practice on reports that you need to use for compliancy on software updates. One of the reports I often use to check the compliancy is
    Compliance 1 - Overall compliance as it provides a good overview of a specific collection for an update group. For more details you can use
    Compliance 3 - Update group (per update).
    My Blog: http://www.petervanderwoude.nl/
    Follow me on twitter: pvanderwoude

  • CSIs best practice to get the best oracle deals

    One company acquired multiple small companies, now time to re-new oracle support , we have 7 CSIs and paying for all of them, 6 of them with quantity 1-2 (and not sure even anybody using it) , any way to check if someone using that CSIs?
    one with quantity 50 (Oracle Database Enterprise Edition) >>this one only DBAs need and using it....
    What the best practice in this case to utilize/get best support @ best price? My idea is to just consolidate all CSIs in one and ask for oracle any discounts...make me right if i am not, new to this topic.....

    I think you can go on MOS for each of those CSI's and create an administrator who can look to see what has been used (if you are asking how to see if anyone is using support). Note that this can be completely misleading, as someone may be using one CSI to, say get patches for all the supported environments. You might also think who needs to get notified, support is pretty inflexible about it.
    I someone disagree with what ajallen said about the purchasing department doing these negotiations, as I have seen, er, a wide variety of ability there. It might be worth it to get one of those license specialist companies, though I wouldn't know which are good. From what I've seen, Oracle will try to charge you to desupport unused licensing, and sometimes do other nasty things if you have hardware on their poop-list (think hp). On the pollyana side, if you can dangle a potential database consolidation sale in front of them, they might play nice.

  • What is the best practice to get thumbnail of photo which I get from cameraUI

    Hi,
    I building a photo sharing application in android and I want to upload a smaller size of photo just after I receive the photo from the cameraUI.
    What is the best practice to resize the photo and to upload it, At the moment I doesn't resize it And the file size is big (1MB) for HTC DESIRE HD ?
    Is it possible to resize the image and keep the EXIF data ?
    Thanks,
    Nimrod.

    Yep,
    Media Manager is the way to go.
    Read the manual aout it.
    It is all explained in there.
    Rienk

  • Best practice for getting all Activities for a Contact

    Hello,
    I am new to the Eloqua API and am wondering what is the best practice for downloading a list of all activities for a given contact.
    Thanks

    Hi Mike,
    For activities in general, Bulk 2.0 Activity Exports will be the best way to go. Docs are here: http://docs.oracle.com/cloud/latest/marketingcs_gs/OMCBB/index.html
    But it can be a complex process to wrap your head around if you're new to the Eloqua API. So if you're in a pinch and don't care about the association of those activities to campaigns, and only need to pull activities for a few contacts, you can resort to using REST API calls.
    The activity calls are visible (from Firebug or Chrome console) if you open any contact record and navigate to the "Activity Log" tab. If you have it set to all activities, it will fire off a dozen or more calls or you can choose an individual one from the picklist to inspect that call in more detail.
    Best regards,
    Bojan

  • What is the best practice to get database connection?

    What are best best practices in database connection to follow?

    &#24335; &#21487; &#20197; &#37319; &#29992;Class.forName &#26041; &#27861; &#26174; &#31034; &#21152; &#36733;&#65292; &#22914; &#19979; &#38754; &#30340; &#35821; &#21477; &#21152; &#36733;Sun &#20844; &#21496; &#30340;JDBC-ODBCbridge &#39537; &#21160; &#31243; &#24207;&#65306;
    Class.forName&#65288;�sun.jdbc.odbc.JdbcOdbcDriver�)&#65307;
    &#28982; &#21518; &#36816; &#29992;DriverManager &#31867; &#30340;getConnection &#26041; &#27861; &#24314; &#31435; &#19982; &#25968; &#25454; &#28304; &#30340; &#36830; &#25509;&#65306;
    Connectioncon=DrivenManagerget-
    Connection(url);
    &#35813; &#35821; &#21477; &#19982;url &#23545; &#35937; &#25351; &#23450; &#30340; &#25968; &#25454; &#28304; &#24314; &#31435; &#36830; &#25509;&#12290; &#33509; &#36830; &#25509; &#25104; &#21151;&#65292; &#21017; &#36820; &#22238; &#19968; &#20010;Connection &#31867; &#30340; &#23545; &#35937;con&#12290; &#20197; &#21518; &#23545; &#36825; &#20010; &#25968; &#25454; &#28304; &#30340; &#25805; &#20316; &#37117; &#26159; &#22522; &#20110;con &#23545; &#35937; &#30340;&#12290;
    &#25191; &#34892; &#26597; &#35810; &#35821; &#21477;&#12290; &#26412; &#25991; &#20171; &#32461; &#22522; &#20110;Statement &#23545; &#35937; &#30340; &#26597; &#35810; &#26041; &#27861;&#12290; &#25191; &#34892;SQL &#26597; &#35810; &#35821; &#21477; &#38656; &#35201; &#20808; &#24314; &#31435; &#19968; &#20010;Statement &#23545; &#35937;&#12290; &#19979; &#38754; &#30340; &#35821; &#21477; &#24314; &#31435; &#21517; &#20026;guo &#30340;Statement &#23545; &#35937;&#65306;
    Statement guo=con.creatStatement()&#65307;
    &#22312;Statement &#23545; &#35937; &#19978;&#65292; &#21487; &#20197; &#20351; &#29992;execQuery &#26041; &#27861; &#25191; &#34892; &#26597; &#35810; &#35821; &#21477;&#12290;execQuery &#30340; &#21442; &#25968; &#26159; &#19968; &#20010;String &#23545; &#35937;&#65292; &#21363; &#19968; &#20010;SQL &#30340;Select &#35821; &#21477;&#12290; &#23427; &#30340; &#36820; &#22238; &#20540; &#26159; &#19968; &#20010;ResultSet &#31867; &#30340; &#23545; &#35937;&#12290;
    ResultSet result=guo.execQuery(�SELECT*FROM A�)
    &#35813; &#35821; &#21477; &#23558; &#22312;result &#20013; &#36820; &#22238;A &#20013; &#30340; &#25152; &#26377; &#34892;&#12290;
    &#23545;Result &#23545; &#35937; &#36827; &#34892;&#65288; &#19979; &#36716;76 &#39029;&#65289;( &#19978; &#25509;73 &#39029;&#65289; &#22788; &#29702; &#21518;&#65292; &#25165; &#33021; &#23558; &#26597; &#35810; &#32467; &#26524; &#26174; &#31034; &#32473; &#29992; &#25143;&#12290;Result &#23545; &#35937; &#21253; &#25324; &#19968; &#20010; &#30001; &#26597; &#35810; &#35821; &#21477; &#36820; &#22238; &#30340; &#19968; &#20010; &#34920;&#65292; &#36825; &#20010; &#34920; &#20013; &#21253; &#21547; &#25152; &#26377; &#30340; &#26597; &#35810; &#32467; &#26524;&#12290; &#23545;Result &#23545; &#35937; &#30340; &#22788; &#29702; &#24517; &#39035; &#36880; &#34892;&#65292; &#32780; &#23545; &#27599; &#19968; &#34892; &#20013; &#30340; &#21508; &#20010; &#21015;&#65292; &#21487; &#20197; &#25353; &#20219; &#20309; &#39034; &#24207; &#36827; &#34892; &#22788; &#29702;&#12290;Result &#31867; &#30340;getXXX &#26041; &#27861; &#21487; &#23558; &#32467; &#26524; &#38598; &#20013; &#30340;SQL &#25968; &#25454; &#31867; &#22411; &#36716; &#25442; &#20026;Java &#25968; &#25454; &#31867; &#22411;

  • Best Practive to resturn Resultset to a JSP page

    I'm writing a class to return a resultSet to use in a JSP page or at least i think thats what i want to do. Whats the best practice to return that resultset.....As the class is written below, when i try to iterate through the resultset in the jsp page i get a NullPointerException which i understand because i closed the connection before returning....If i take out all the code in the finally section i can iterate through the data no problem....but thats not good to leave connections open.......instead of returning a resultset should i return some other type such as an Array with all the data. How should i return the data?
    ======================================================================
    package foo;
    import javax.naming.*;
    import javax.sql.*;
    import java.sql.*;
    public class DBTest{
    String foo = "Not Connected";
    String bar = "Not";
    public ResultSet getUser(){
    Connection conn = null;
    Statement stmt = null; //Or preparedStatement if needed
    ResultSet rs = null;
    try{
    Context ctx = new InitialContext();
    if(ctx == null)
    throw new Exception("Boom - No Context");
    DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Show");
    if(ds != null){
    conn = ds.getConnection();
    if(conn != null){
    foo = "Got Connection " + conn.toString();
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT * FROM user");
    if(rs.next()){
    foo=rs.getString("login");
    bar=rs.getString("password");
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    // Always make sure result sets and statements are closed,
    // and the connection is returned to the pool.
    if(rs != null){
    try{
    rs.close();
    }catch(SQLException e){;}
    rs=null;
    if(stmt != null){
    try{stmt.close();}catch(SQLException e){;}
    stmt=null;
    if(conn != null){
    try{conn.close();}catch(SQLException e){;}
    conn=null;
    return rs;
    public String getFoo(){return foo;}
    public String getBar(){return bar;}

    Yes, when you close your connection all data in your result set is lost, and yes its not a good idea to keep connections open. Its also not a good idea to pass results around. What you should do is store the data in some type of collection object and pass that to your jsp.
    for exmaple if you select data from a table that stores names of people, e.g. two columns first_name and last_name, then create a class called People with two private string members to hold the first and last names. loop through your result set and build a People object for each row of data. when your done close the result set (and the connection if you want). store all your People objects is a collection (vector, array, whatever). pass the vector to your jsp for display.
    -S-

  • Not Prompting for Parameters in .jsp page

    Post Author: montgomm
    CA Forum: General
    We have defined an input parameter to our report and it will successfully prompt for the input parameter within the designer preview.  However, once we build a .jsp page to contain the report and load it to our server, when we run it, it never prompts for any parameters. It does display the report in the .jsp, but it appears to be always running for the last parameters that we entered for the report in the designer.Any suggestions on correcting this?Is there any good documentation on parameter passing in .jsp pages?

    Yes.
    You can create a form in one jsp and have it's value sent in a parameter key to another jsp file identified as the action target.
    Here is code for first jsp file (i.e. first.jsp). This will give you a text input box and a submit button labeled sendit.
    <form action=second.jsp>
    <input type=text name=filename>
    <input type=submit name=Submit value="SendIT">
    </form>
    Here is code snibit to get the param in the second jsp file (ie. second.jsp)
    StringBuffer theparam = new StringBuffer();
    theparam.append(request.getParameter("filename"));
    out.println("The second jsp got param=" +theparam);
    Hope you get it workin'
    Gregg

  • How to get multiple selections from jsp page in my servlet

    Hello Everyone,
    I've a list that allows users to make multiple selections.
    <select name=location multiple="multiple">
        <option>
             All
        </option>
        <option>
             Hyd
        </option>
        <option>
             Dub
        </option>
        <option>
             Mtv
        </option>
      </select>I want to get the selections made by user in jsp page from my servlet, selections can be multiple too.
    ArrayList locList = new ArrayList();
    locList = request.getParameter("location");when I do so, I get compilation error as the request returns string type. How do I then get multiple selections made by the user.
    Please let me know.

    For those kind of basic questions it would help a lot if you just gently consult the javadocs and tutorials.
    HttpServletRequest API: [http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html]
    Java EE tutorial part II: [http://java.sun.com/javaee/5/docs/tutorial/doc/]
    Coreservlet tutorials: [http://courses.coreservlets.com/Course-Materials/]

  • Problem in getting an object in JSP page.

    Hi experts,
    I am forming a javabean object in the controller(servlet) and passing the object to a JSP page by setting that object as an attribute. But in that jsp page, when i compile, its not able to locate the class. At the beginning itself while typecasting the attribute as that bean object, i get an error. All the servlet, bean files are in the default package only.
    This is how i used..
    <%
    String m = (String) request.getAttribute("mode");
    //My bean object below.
    adjuster_bean res=(adjuster_bean)request.getAttribute("data");
    %>
    And i am using like, calling the bean object's get method to set values of the text boxes in the JSP page. I need help please....

    Vigsen wrote:
    Hi experts,
    I am forming a javabean object in the controller(servlet) and passing the object to a JSP page by setting that object as an attribute. But in that jsp page, when i compile, its not able to locate the class. At the beginning itself while typecasting the attribute as that bean object, i get an error. All the servlet, bean files are in the default package only.
    This is how i used..
    <%
    String m = (String) request.getAttribute("mode");
    //My bean object below.
    adjuster_bean res=(adjuster_bean)request.getAttribute("data");
    %>
    And i am using like, calling the bean object's get method to set values of the text boxes in the JSP page. I need help please....Are you importing the bean class in your jsp? Something like this...
    <%@ page import="java.util.*, yourpackage.YourBeanClass" %> Secondly, I see standards not followed in your code...
    a) Firstly, as someone else pointed out in this same post. Avoid as much as possible using java scriplets in JSPs. Instead try use JSTL and EL expressions.
    b) A java class name should always start with capital letter for every word in the class name and with no underscores. Your bean class name (adjuster_bean) should be changed to something like AdjusterBean
    If you want to become a good Java programmer then standards are essential. But, again, it is totally up to you to follow.

Maybe you are looking for

  • Getting error while submitting data to excel workbook through excel services in InfoPath 2010

    Hi, I have a requirement where in have to fetch the calculated values from the excel workbook through InfoPath 2010 form. When I am submitting cell value to excel workbook using SetCellA1 by executing following action: "Submit using data connection:

  • HP Laserjet Print Queue Stuck after completed

    Hello, I am working with on a client of mine's brand new iMac, and everything is working good except when she prints to her HP Laserjet P2035, the job completes just fine, but sits in the queue for about 20-30 seconds before completing itself. At thi

  • G5 to mac mini

    I have a Mac Mini and would like to network it to my G5. What do I do to do this? Can I connect with Firewire? thanks

  • List Display alignment

    Hi all, I am writing a list report which is using a normal WRITE statement in Japanese language (Program Original Language = JA). However, I have the alignment problem as below: Customer No  |  Customer Name                | Accounting Document No |

  • No sync option

    Ipod is connected and both the computer and itunes detect it. just can't get it to sync.... and it's not mine orin my possesion so I don't have anymore details but anything anyone could tell me would be great.